7

I have a requirement to edit my HttpResponse to add header value. I was searching for single point to implement it and found that ResponseBodyadvice would be helpful.

But both methods That overrode in my new class was never called. Could you please tell if I have missed any configurations.

@ControllerAdvice
public class EditResponseHeader implements ResponseBodyAdvice<object> {


@Override
public boolean supports(MethodParameter returnType,
        Class<? extends HttpMessageConverter<?>> converterType) {

   return true;
}

....

@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType,
        MediaType selectedContentType,
        Class<? extends HttpMessageConverter<?>> selectedConverterType,
        ServerHttpRequest request, ServerHttpResponse response) {

    ....

    return body;
}

}
Aurasphere
  • 3,841
  • 12
  • 44
  • 71
Ultimata
  • 71
  • 1
  • 1
  • 5
  • All my controllers are having @RequestMapping annotation and mvc is annotation driven. I am also using MappingJackson2Json as default views – Ultimata Jul 23 '16 at 15:21

1 Answers1

1

What about :

@Override
@ExceptionHandler(Exception.class)
public Response<?> beforeBodyWrite(Response<?> body, MethodParameter returnType,
                                   MediaType selectedContentType,
                                   Class<? extends HttpMessageConverter<?>> selectedConverterType,
                                   ServerHttpRequest request, ServerHttpResponse response) {

                ....

    return body;
}
Norbert Bicsi
  • 1,562
  • 2
  • 19
  • 33
AchillesVan
  • 4,156
  • 3
  • 35
  • 47
  • How would changing return type of beforeBodyWrite to Response> from object matter ? – Ultimata Jul 23 '16 at 15:34
  • may the following link help you find a solution. http://stackoverflow.com/questions/21884737/controlleradvice-not-firing – AchillesVan Jul 23 '16 at 16:13
  • 3
    When I write any method annotated with `@InitBinder` or `@ExceptionHandler` then it's working fine, but `@ExceptionHandler` is only called when exception is thrown and `@initBinder` is called before controller is executed. My requirement is to call this method after every controller is executed not just when exception is thrown. – Ultimata Jul 23 '16 at 17:06
  • Try the HandlerInterceptor interface. HandlerInterceptor interface defines the following methods: § preHandle – this method is executed before the controller processes the request. § postHandle – this method is executed after the controller processes the request, but before the view is rendered by the DispatcherServlet. § afterCompletion – this method is invoked after the completion of request processing (that is, after the view is rendered by the DispatcherServlet) to do any cleanup, if required. – AchillesVan Jul 23 '16 at 17:37
  • Here an concrete example: http://www.concretepage.com/spring/spring-mvc/spring-handlerinterceptor-annotation-example-webmvcconfigureradapter – AchillesVan Jul 23 '16 at 17:43
  • I had tried using `@postHandle` of HandlerInterceptor, but it's also not working. I also read some docs where it's mentioned that `@postHandle` doesn't work well with `@ResponseBody` – Ultimata Jul 25 '16 at 09:35
  • 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. – Ultimata Jul 25 '16 at 09:35