Search code examples
javarestapiobserver-patternobservers

Observer implementation for APIs in java


I've got several APIs defined in this java project. I need something to observe all these APIs so that whenever an API is called, I can fetch the response status code from the APIs. My intention is to try to not touch the API code and have some kind of mechanism to observe the API calls and fetch the response status code returned from the API using the observer.


Solution

  • You can use interceptors on the classes from where you invoke these APIs using annotation. The Around invoke method on the interceptor will be called whenever the method on the calling class is invoked. you can get the response status and and do any addional processing in the around invoke method in the interceptor. You have the option to exlude the interceptor at method level on the calling class.

    Please see for example. http://www.byteslounge.com/tutorials/java-ee-ejb-interceptors-tutorial-and-example