V_Singh V_Singh - 1 year ago 172
HTTP Question

Java interceptor to process the REST API response code

I have a requirement to intercept the REST API responses for out application and do something with them. One very obvious approach to achieve this is to define an annotation and interceptor at method level (so that it can be applied to the REST API methods). However, I could not find a way to extract/intercept the response code for API responses. I am very new to Java EE world, so might be missing something here, but didn't find anything on internet search either. Our application is based on standard JavaEE with CXF.

I have seen some examples with code similar to the following, but not sure how I can get the API response from that. Any help would be appreciated.

public Object around(InvocationContext context) throws Exception {......

Answer Source

Suppose you are using the standard JEE solution, from the doc

public Object logInvocation(InvocationContext ctx) throws Exception {
   String class = ctx.getMethod().getDeclaringClass().getName();
   String method = ctx.getMethod().getName();
   Logger.global.entering(class, method, ctx.getParameters());
   try {
      Object result = ctx.proceed();
      Logger.global.exiting(class, method, result);
      return result;
   catch (Exception e) {
      Logger.global.throwing(class, method, e);
      throw e;


The Object result = ctx.proceed(); is the result for you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download