spring mvc interceptor: access to ResponseEntity in postHandle

Go To StackoverFlow.com


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

2012-04-04 20:26
by jason


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

2012-04-19 22:02
by jason
I solved it using this, Jason. [https://stackoverflow.com/questions/26756811/post-processing-of-a-json-response-in-spring-mvc - Lucas 2017-05-24 17:14


By the time you wrote your question I think it was not possible but now it is. From their docs http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-handlermapping-intercepto

"Note that the postHandle method of HandlerInterceptor is not always ideally suited for use with @ResponseBody and ResponseEntity methods. In such cases an HttpMessageConverter writes to and commits the response before postHandle is called which makes it impossible to change the response, for example to add a header. Instead an application can implement ResponseBodyAdvice and either declare it as an @ControllerAdvice bean or configure it directly on RequestMappingHandlerAdapter"

2016-07-20 11:34
by Jordi Llach


public class ResponseDTOFilterAdvice implements ResponseBodyAdvice<Object> {
    public boolean supports(final MethodParameter returnType, final Class<? extends HttpMessageConverter<?>> converterType) {
        return true;

    public Object beforeBodyWrite(final Object body, final MethodParameter returnType, final MediaType selectedContentType,
        final Class<? extends HttpMessageConverter<?>> selectedConverterType, final ServerHttpRequest request,
        final ServerHttpResponse response) {
        if (body instanceof ResponseDTO<?>) {
            ResponseDTO<?> responseDTO = (ResponseDTO<?>) body;
        return body;

Source: Post processing of a Json response in spring MVC

2017-05-24 17:48
by Lucas