Class JacksonJsonHttpMessageConverter

All Implemented Interfaces:
HttpMessageConverter<Object>, SmartHttpMessageConverter<Object>

public class JacksonJsonHttpMessageConverter extends AbstractJacksonHttpMessageConverter<tools.jackson.databind.json.JsonMapper>
Implementation of HttpMessageConverter that can read and write JSON using Jackson 3.x's JsonMapper.

This converter can be used to bind to typed beans, or untyped HashMap instances.

By default, this converter supports application/json and application/*+json with UTF-8 character set. This can be overridden by setting the supportedMediaTypes property.

The default constructor loads JacksonModules found by MapperBuilder.findModules(ClassLoader).

The following hints entries are supported:

  • A JSON view with a com.fasterxml.jackson.annotation.JsonView key and the class name of the JSON view as value.
  • A filter provider with a tools.jackson.databind.ser.FilterProvider key and the filter provider class name as value.
Since:
7.0
Author:
Sebastien Deleuze
  • Constructor Details

    • JacksonJsonHttpMessageConverter

      public JacksonJsonHttpMessageConverter()
      Construct a new instance with a JsonMapper customized with the JacksonModules found by MapperBuilder.findModules(ClassLoader) and ProblemDetailJacksonMixin.
    • JacksonJsonHttpMessageConverter

      public JacksonJsonHttpMessageConverter(tools.jackson.databind.json.JsonMapper objectMapper)
      Construct a new instance with the provided JsonMapper.
      See Also:
      • JsonMapper.builder()
      • MapperBuilder.findModules(ClassLoader)
  • Method Details

    • setJsonPrefix

      public void setJsonPrefix(String jsonPrefix)
      Specify a custom prefix to use for this view's JSON output. Default is none.
      See Also:
    • setPrefixJson

      public void setPrefixJson(boolean prefixJson)
      Indicate whether the JSON output by this view should be prefixed with ")]}', ". Default is false.

      Prefixing the JSON string in this manner is used to help prevent JSON Hijacking. The prefix renders the string syntactically invalid as a script so that it cannot be hijacked. This prefix should be stripped before parsing the string as JSON.

      See Also:
    • getMediaTypesForProblemDetail

      protected List<MediaType> getMediaTypesForProblemDetail()
      Description copied from class: AbstractJacksonHttpMessageConverter
      Return the supported media type(s) for ProblemDetail. By default, an empty list, unless overridden in subclasses.
      Overrides:
      getMediaTypesForProblemDetail in class AbstractJacksonHttpMessageConverter<tools.jackson.databind.json.JsonMapper>
    • writePrefix

      protected void writePrefix(tools.jackson.core.JsonGenerator generator, Object object)
      Description copied from class: AbstractJacksonHttpMessageConverter
      Write a prefix before the main content.
      Overrides:
      writePrefix in class AbstractJacksonHttpMessageConverter<tools.jackson.databind.json.JsonMapper>
      Parameters:
      generator - the generator to use for writing content.
      object - the object to write to the output message