我需要编辑我的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;
}
}
答案 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;
}