spring mvc interceptor: access to ResponseEntity in postHandle

interceptor, rest, spring-mvc

Solution

I have found one full solution, and one partial solution.

Partial solution: For those that are using Servlet 3.0, the HTTPStatus Code is available on the HttpServletResponse object. This would have solved half of my problem in that I can get the status code, but it still didn't give me access to the ResponseEntity which has the body attribute that I wanted to inspect.

Full Solution (works on Servlet 2.x): I used a combination of an Aspect and an Interceptor.

The Aspect was coded to target the @AfterReturn of the controller's methods that return ResponseEntities. It basically captured the returned responseEntity and put it in a ThreadLocal<ResponseEntity> collection.

The Interceptor then asked the Aspect for the thread's response and TA-DAH it was accessible.

I hope this answer helps someone else. Cheers, Jason

Problem

I have an interceptor that logs the outcome of Spring MVC responses. All my responses return ResponseEntity objects that have JSON body contents. I would like to grab both the response body and the http status from the ResponseEntity. How can I get access to the ResponseEntity from the postHandle method attributes? ``` public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {} ``` The modelAndView attribute is null for my invocations. Thanks, Jason

Original source

Related problems