I have controllers that return JSON to the client. The controllers methods are marked using mvc annotation such as:
@RequestMapping("/delete.me")
public @ResponseBody Map<String, Object> delete(HttpServletRequest request, @RequestParam("ids[]") Integer[] ids) {
Spring knows to return JSON since Jackson is on the class path and the client is requesting a JSON response. I would like to log the response of these requests and all other controllers. In the past I have used an interceptor to do this. However, I got the response body from the ModelAndView. How can I get the response body in the inteceptor now that I’m using @ResponseBody? Specifically, how can I get the response body in this method?
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
You can log everything by using
CustomizableTraceInterceptoryou can either set it in your application context xml config and use AOP: (log level Trace)
or you can completly customize it by implementing it in Java and use the method setExitMessage():
and use the placeholders such as ‘$[returnValue]’. You can find the complete list in the spring api documentation.
EDIT: Also, if you want to get the value of your @ResponseBody in another interceptor, I think it’s not possible until version > 3.1.1. Check this issue: https://jira.springsource.org/browse/SPR-9226