Caznik
Caznik

Reputation: 49

Using interceptor to log REST service but can't log request and response

I have some REST service and I want to log the request and response.They receive a JSON request and generate another JSON response. I have created an interceptor but I'm not being able to get the request/response body. This is my cxf configuration:

<bean id="ContextInInterceptor" class="main.interceptors.ContextMessageInInterceptor"></bean>
<bean id="ContextOutInterceptor" class="main.interceptors.ContextMessageOutInterceptor"></bean>
<bean id="ContextJSONInInterceptor" class="main.interceptors.ContextJSONInInterceptor"></bean>
<bean id="ContextJSONOutInterceptor" class="main.interceptors.ContextJSONOutInterceptor"></bean>

<cxf:bus>
   <cxf:features>
      <cxf:logging/>
   </cxf:features>

   <cxf:inInterceptors>
      <ref bean="ContextInInterceptor"/>
   </cxf:inInterceptors>
   <cxf:outInterceptors>
      <ref bean="ContextOutInterceptor"/>
   </cxf:outInterceptors>
</cxf:bus>

And this is my interceptors code:

public class ContextMessageInInterceptor extends AbstractPhaseInterceptor<org.apache.cxf.message.Message> {

       @Autowired
       ContextJSONInInterceptor contextJSONInInterceptor;

       public ContextMessageInInterceptor() {
           super(Phase.PRE_INVOKE);
       }

       @Override
       public void handleMessage(Message message) {
           contextJSONInInterceptor.handleMessage(message);
       }
}

public class ContextJSONInInterceptor extends AbstractPhaseInterceptor<Message> {

    public ContextJSONInInterceptor() {
        super(Phase.PRE_INVOKE);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        System.out.println("Content-Type: " + message.get(Message.CONTENT_TYPE));
        System.out.println(message.toString());
        System.out.println("---------------");
    }
}

public class ContextMessageOutInterceptor extends AbstractPhaseInterceptor<Message> {

    @Autowired
    ContextJSONOutInterceptor contextJSONOutInterceptor;

    public ContextMessageOutInterceptor() {
        super(Phase.WRITE);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        contextJSONOutInterceptor.handleMessage(message);
    }
}

public class ContextJSONOutInterceptor extends AbstractPhaseInterceptor<Message> {

    public ContextJSONOutInterceptor() {
        super(Phase.WRITE);
    }

    @Override
    public void handleMessage(Message message) throws Fault {
        System.out.println("Content-Type: " + Message.CONTENT_TYPE);
        System.out.println(message.toString());
        System.out.println("---------------");
    }

}

ContextMessageInInterceptor and ContextMessageOutInterceptor works properly and they go to ContextJSONInInterceptor and ContextJSONOutInterceptor respectively, but I don't know how to get the request/reponse body from those interceptors. I have tried to use the "Message" constants and the content-type is all I get.

Anyone knows how to get the request/reponse body from those interceptors?

Thanks a lot for your time.

Upvotes: 1

Views: 1238

Answers (2)

dgarceran
dgarceran

Reputation: 187

You have two options: with HandlerInterceptor you can see the requests and methods, with some reflection you could get some information that maybe is everything you need.

public class ApiInterceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    // code
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//code
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
   // code
    }
}

The second option is Spring AOP (which could be complementary). You could use a pointcut that would target all the @RequestMapping() annotations, and an advice that would be executed in the exact moment you want to. I asked (and answered) a question about this not too long ago.

Upvotes: 0

Suvansh
Suvansh

Reputation: 264

Rather than interceptors,AOP would be a good option. You can apply it on your controller methhods where request/response traffic passes.And for getting the request object in that request scope you can do something like:

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
            .getRequest();

Upvotes: 1

Related Questions