如何将ResponseBodyAdvice实现到所有控制器?

时间:2016-07-23 15:06:31

标签: java spring model-view-controller response.addheader

我需要编辑我的HttpResponse以添加标头值。我正在寻找单点来实现它,并发现ResponseBodyadvice会有所帮助。

但是我的新课程中的两种方法都没有被调用过。你能否告诉我是否错过了任何配置。

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

}

1 个答案:

答案 0 :(得分:0)

怎么样:

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

                ....

    return body;
}