public class JsonViewResponseBodyAdvice extends AbstractMappingJacksonResponseBodyAdvice
ResponseBodyAdvice
implementation that adds support for Jackson's
@JsonView
annotation declared on a Spring MVC @RequestMapping
or @ExceptionHandler
method.
The serialization view specified in the annotation will be passed in to the
MappingJackson2HttpMessageConverter
which will then use it to serialize the response body.
Note that despite @JsonView
allowing for more than one class to
be specified, the use for a response body advice is only supported with
exactly one class argument. Consider the use of a composite interface.
JsonView
,
ObjectMapper.writerWithView(Class)
Constructor and Description |
---|
JsonViewResponseBodyAdvice() |
Modifier and Type | Method and Description |
---|---|
protected void |
beforeBodyWriteInternal(MappingJacksonValue bodyContainer,
MediaType contentType,
MethodParameter returnType,
ServerHttpRequest request,
ServerHttpResponse response)
Invoked only if the converter type is
MappingJackson2HttpMessageConverter . |
boolean |
supports(MethodParameter returnType,
Class<? extends HttpMessageConverter<?>> converterType)
Whether this component supports the given controller method return type
and the selected
HttpMessageConverter type. |
beforeBodyWrite, getOrCreateContainer
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType)
ResponseBodyAdvice
HttpMessageConverter
type.supports
in interface ResponseBodyAdvice<Object>
supports
in class AbstractMappingJacksonResponseBodyAdvice
returnType
- the return typeconverterType
- the selected converter typetrue
if ResponseBodyAdvice.beforeBodyWrite(T, org.springframework.core.MethodParameter, org.springframework.http.MediaType, java.lang.Class<? extends org.springframework.http.converter.HttpMessageConverter<?>>, org.springframework.http.server.ServerHttpRequest, org.springframework.http.server.ServerHttpResponse)
should be invoked;
false
otherwiseprotected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType, MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response)
AbstractMappingJacksonResponseBodyAdvice
MappingJackson2HttpMessageConverter
.beforeBodyWriteInternal
in class AbstractMappingJacksonResponseBodyAdvice