Class JsonViewResponseBodyAdvice

java.lang.Object
org.springframework.web.servlet.mvc.method.annotation.AbstractMappingJacksonResponseBodyAdvice
org.springframework.web.servlet.mvc.method.annotation.JsonViewResponseBodyAdvice
All Implemented Interfaces:
ResponseBodyAdvice<Object>

public class JsonViewResponseBodyAdvice extends AbstractMappingJacksonResponseBodyAdvice
A 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.

Since:
4.1
Author:
Rossen Stoyanchev
See Also:
  • JsonView
  • ObjectMapper.writerWithView(Class)
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    beforeBodyWriteInternal(org.springframework.http.converter.json.MappingJacksonValue bodyContainer, org.springframework.http.MediaType contentType, org.springframework.core.MethodParameter returnType, org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response)
    Invoked only if the converter type is MappingJackson2HttpMessageConverter.
    determineWriteHints(@Nullable Object body, org.springframework.core.MethodParameter returnType, org.springframework.http.MediaType selectedContentType, Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> selectedConverterType)
    Invoked to determine write hints if the converter is a SmartHttpMessageConverter.
    boolean
    supports(org.springframework.core.MethodParameter returnType, Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> converterType)
    Whether this component supports the given controller method return type and the selected HttpMessageConverter type.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • JsonViewResponseBodyAdvice

      public JsonViewResponseBodyAdvice()
  • Method Details

    • supports

      public boolean supports(org.springframework.core.MethodParameter returnType, Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> converterType)
      Description copied from interface: ResponseBodyAdvice
      Whether this component supports the given controller method return type and the selected HttpMessageConverter type.
      Specified by:
      supports in interface ResponseBodyAdvice<Object>
      Overrides:
      supports in class AbstractMappingJacksonResponseBodyAdvice
      Parameters:
      returnType - the return type
      converterType - the selected converter type
      Returns:
      true if ResponseBodyAdvice.beforeBodyWrite(T, MethodParameter, MediaType, Class, ServerHttpRequest, ServerHttpResponse) should be invoked; false otherwise
    • beforeBodyWriteInternal

      protected void beforeBodyWriteInternal(org.springframework.http.converter.json.MappingJacksonValue bodyContainer, org.springframework.http.MediaType contentType, org.springframework.core.MethodParameter returnType, org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response)
      Description copied from class: AbstractMappingJacksonResponseBodyAdvice
      Invoked only if the converter type is MappingJackson2HttpMessageConverter.
      Specified by:
      beforeBodyWriteInternal in class AbstractMappingJacksonResponseBodyAdvice
    • determineWriteHints

      public @Nullable Map<String,Object> determineWriteHints(@Nullable Object body, org.springframework.core.MethodParameter returnType, org.springframework.http.MediaType selectedContentType, Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> selectedConverterType)
      Description copied from interface: ResponseBodyAdvice
      Invoked to determine write hints if the converter is a SmartHttpMessageConverter.
      Parameters:
      body - the body to be written
      returnType - the return type of the controller method
      selectedContentType - the content type selected through content negotiation
      selectedConverterType - the converter type selected to write to the response
      Returns:
      the hints determined otherwise null