0

I want to intercept Spring servlet response just at the end of the request. I mean, what i want to do is to log service response, that json which services returns and the statusCode. That is all what i want to log.

For that, i've already tried using HandlerInterceptorAdapter like this

public class ResponseLoggerInterceptor
    extends HandlerInterceptorAdapter {

    private static final Logger LOGGER = LoggerFactory.getLogger(ResponseLoggerInterceptor.class);

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
        LOGGER.info("Intercepted");
        super.postHandle(request, response, handler, modelAndView);
    }

}

That "handler" object is the object respose, but it is not serialized. Do you know where and how i have to intercept the very last servlet response? Thanks in advance

jscherman
  • 5,839
  • 14
  • 46
  • 88

1 Answers1

1

You will need a 'HttpResponse' object And will suggest that you do it in the 'afterCompletion' method

iamiddy
  • 3,015
  • 3
  • 30
  • 33
  • What do you mean by need a HattpResponse? Where does it appears? Thanks for your answer – jscherman May 27 '15 at 20:35
  • Override the afterCompletion method – iamiddy May 27 '15 at 20:38
  • public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception – iamiddy May 27 '15 at 20:39
  • Are you trying to say that i have the response? Yes, i have it. Is the ResponseEntity object, and i know i have it, but that is not serialized yet. What i want is that object serialized (and i don't think that would be appropiate to serialize it by myself) – jscherman May 27 '15 at 20:41
  • I assume you want to log the response? – iamiddy May 27 '15 at 20:43
  • Yes that is exactly what i want to do – jscherman May 27 '15 at 20:58
  • 1
    Ok, good then override the afterCompletion method, and if you want to log a json response here is [SO](http://stackoverflow.com/questions/3242236/capture-and-log-the-response-body) for that – iamiddy May 27 '15 at 21:28