Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement ResponseBodyAdvice to all controllers?

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;
}

}
like image 359
Ultimata Avatar asked Jul 23 '16 15:07

Ultimata


1 Answers

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;
}
like image 58
AchillesVan Avatar answered Nov 14 '22 23:11

AchillesVan