For all my RestControllers handlers, which all return a subtype of MyResponseType
, I would like to write a Spring ResponseBodyAdvice
which manipulates the response based on some @ModelAttribute
. However, I don't know how to access the ModelView
at this place.
@ControllerAdvice
public class MyGoldenAdvice implements ResponseBodyAdvice<MyResponseType> {
@Override
public boolean supports(final MethodParameter returnType, final Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
@Override
public RepresentationModel<?> beforeBodyWrite(
final MyResponseType body,
final MethodParameter returnType,
final MediaType selectedContentType,
final Class<? extends HttpMessageConverter<?>> selectedConverterType,
final ServerHttpRequest request,
final ServerHttpResponse response) {
// here I would like to access the ModelView
return body;
}
}
Unfortunately, since I have to implement the interface ResponseBodyAdvice
, I cannot have @ModelAttribute
arguments.
Of course, I could also use an interceptor. However, there the response is already written, thus I could only manipulate the JSON of my MyResponseType
instance.
Any idea?