Class AbstractJacksonHttpMessageConverter<T extends tools.jackson.databind.ObjectMapper>

Type Parameters:
T - the type of ObjectMapper
All Implemented Interfaces:
HttpMessageConverter<Object>, SmartHttpMessageConverter<Object>
Direct Known Subclasses:
JacksonCborHttpMessageConverter, JacksonJsonHttpMessageConverter, JacksonSmileHttpMessageConverter, JacksonXmlHttpMessageConverter, JacksonYamlHttpMessageConverter

public abstract class AbstractJacksonHttpMessageConverter<T extends tools.jackson.databind.ObjectMapper> extends AbstractSmartHttpMessageConverter<Object>
Abstract base class for Jackson based and content type independent HttpMessageConverter implementations.

The following hint 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
See Also:
  • Field Details

    • defaultMapper

      protected final T extends tools.jackson.databind.ObjectMapper defaultMapper
  • Constructor Details

    • AbstractJacksonHttpMessageConverter

      protected AbstractJacksonHttpMessageConverter(tools.jackson.databind.cfg.MapperBuilder<T,?> builder, MediaType supportedMediaType)
      Construct a new instance with the provided builder customized with the JacksonModules found by MapperBuilder.findModules(ClassLoader) and MediaType.
    • AbstractJacksonHttpMessageConverter

      protected AbstractJacksonHttpMessageConverter(tools.jackson.databind.cfg.MapperBuilder<T,?> builder, MediaType... supportedMediaTypes)
      Construct a new instance with the provided builder customized with the JacksonModules found by MapperBuilder.findModules(ClassLoader) and MediaTypes.
    • AbstractJacksonHttpMessageConverter

      protected AbstractJacksonHttpMessageConverter(T mapper)
      Construct a new instance with the provided ObjectMapper.
    • AbstractJacksonHttpMessageConverter

      protected AbstractJacksonHttpMessageConverter(T mapper, MediaType supportedMediaType)
      Construct a new instance with the provided ObjectMapper and MediaType.
    • AbstractJacksonHttpMessageConverter

      protected AbstractJacksonHttpMessageConverter(T mapper, MediaType... supportedMediaTypes)
      Construct a new instance with the provided ObjectMapper and MediaTypes.
  • Method Details

    • setSupportedMediaTypes

      public void setSupportedMediaTypes(List<MediaType> supportedMediaTypes)
      Description copied from class: AbstractHttpMessageConverter
      Set the list of MediaType objects supported by this converter.
      Overrides:
      setSupportedMediaTypes in class AbstractHttpMessageConverter<Object>
    • getMapper

      public T getMapper()
      Return the main ObjectMapper in use.
    • registerMappersForType

      public void registerMappersForType(Class<?> clazz, Consumer<Map<MediaType, T>> registrar)
      Configure the ObjectMapper instances to use for the given Class. This is useful when you want to deviate from the default ObjectMapper or have the ObjectMapper vary by MediaType.

      Note: Use of this method effectively turns off use of the default ObjectMapper and supportedMediaTypes for the given class. Therefore it is important for the mappings configured here to include every MediaType that must be supported for the given class.

      Parameters:
      clazz - the type of Object to register ObjectMapper instances for
      registrar - a consumer to populate or otherwise update the MediaType-to-ObjectMapper associations for the given Class
    • getMappersForType

      public Map<MediaType, T> getMappersForType(Class<?> clazz)
      Return ObjectMapper registrations for the given class, if any.
      Parameters:
      clazz - the class to look up for registrations for
      Returns:
      a map with registered MediaType-to-ObjectMapper registrations, or empty if in case of no registrations for the given class.
    • getSupportedMediaTypes

      public List<MediaType> getSupportedMediaTypes(Class<?> clazz)
      Description copied from interface: HttpMessageConverter
      Return the list of media types supported by this converter for the given class. The list may differ from HttpMessageConverter.getSupportedMediaTypes() if the converter does not support the given Class or if it supports it only for a subset of media types.
      Parameters:
      clazz - the type of class to check
      Returns:
      the list of media types supported for the given class
    • getMediaTypesForProblemDetail

      protected List<MediaType> getMediaTypesForProblemDetail()
      Return the supported media type(s) for ProblemDetail. By default, an empty list, unless overridden in subclasses.
    • canRead

      public boolean canRead(ResolvableType type, @Nullable MediaType mediaType)
      Description copied from interface: SmartHttpMessageConverter
      Indicates whether the given type can be read by this converter. This method should perform the same checks as HttpMessageConverter.canRead(Class, MediaType) with additional ones related to the generic type.
      Specified by:
      canRead in interface SmartHttpMessageConverter<T extends tools.jackson.databind.ObjectMapper>
      Overrides:
      canRead in class AbstractSmartHttpMessageConverter<Object>
      Parameters:
      type - the (potentially generic) type to test for readability. The type source may be used for retrieving additional information (the related method signature for example) when relevant.
      mediaType - the media type to read, can be null if not specified. Typically, the value of a Content-Type header.
      Returns:
      true if readable; false otherwise
    • canWrite

      public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType)
      Description copied from class: AbstractHttpMessageConverter
      This implementation checks if the given class is supported, and if the supported media types include the given media type.
      Specified by:
      canWrite in interface HttpMessageConverter<T extends tools.jackson.databind.ObjectMapper>
      Specified by:
      canWrite in interface SmartHttpMessageConverter<T extends tools.jackson.databind.ObjectMapper>
      Overrides:
      canWrite in class AbstractHttpMessageConverter<Object>
      Parameters:
      clazz - the class to test for writability
      mediaType - the media type to write (can be null if not specified); typically the value of an Accept header.
      Returns:
      true if writable; false otherwise
    • read

      Description copied from interface: SmartHttpMessageConverter
      Read an object of the given type from the given input message, and returns it.
      Parameters:
      type - the (potentially generic) type of object to return. This type must have previously been passed to the canRead method of this interface, which must have returned true. The type source may be used for retrieving additional information (the related method signature for example) when relevant.
      inputMessage - the HTTP input message to read from
      hints - additional information about how to encode
      Returns:
      the converted object
      Throws:
      IOException - in case of I/O errors
      HttpMessageNotReadableException - in case of conversion errors
    • readInternal

      protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException
      Description copied from class: AbstractHttpMessageConverter
      Abstract template method that reads the actual object. Invoked from AbstractHttpMessageConverter.read(Class, HttpInputMessage).
      Overrides:
      readInternal in class AbstractSmartHttpMessageConverter<Object>
      Parameters:
      clazz - the type of object to return
      inputMessage - the HTTP input message to read from
      Returns:
      the converted object
      Throws:
      IOException - in case of I/O errors
      HttpMessageNotReadableException - in case of conversion errors
    • customizeReader

      protected tools.jackson.databind.ObjectReader customizeReader(tools.jackson.databind.ObjectReader reader, tools.jackson.databind.JavaType javaType)
      Subclasses can use this method to customize the ObjectReader used for reading values.
      Parameters:
      reader - the reader instance to customize
      javaType - the type of element values to read
      Returns:
      the customized ObjectReader
    • getCharset

      protected Charset getCharset(@Nullable MediaType contentType)
      Determine the charset to use for JSON input.

      By default this is either the charset from the input MediaType or otherwise UTF-8. Can be overridden in subclasses.

      Parameters:
      contentType - the content type of the HTTP input message
      Returns:
      the charset to use
    • writeInternal

      protected void writeInternal(Object object, ResolvableType resolvableType, HttpOutputMessage outputMessage, @Nullable Map<String,Object> hints) throws IOException, HttpMessageNotWritableException
      Description copied from class: AbstractSmartHttpMessageConverter
      Abstract template method that writes the actual body. Invoked from AbstractSmartHttpMessageConverter.write(Object, ResolvableType, MediaType, HttpOutputMessage, Map).
      Specified by:
      writeInternal in class AbstractSmartHttpMessageConverter<Object>
      Parameters:
      object - the object to write to the output message
      resolvableType - the type of object to write
      outputMessage - the HTTP output message to write to
      hints - additional information about how to encode
      Throws:
      IOException - in case of I/O errors
      HttpMessageNotWritableException - in case of conversion errors
    • customizeWriter

      protected tools.jackson.databind.ObjectWriter customizeWriter(tools.jackson.databind.ObjectWriter writer, @Nullable tools.jackson.databind.JavaType javaType, @Nullable MediaType contentType)
      Subclasses can use this method to customize the ObjectWriter used for writing values.
      Parameters:
      writer - the writer instance to customize
      javaType - the type of element values to write
      contentType - the selected media type
      Returns:
      the customized ObjectWriter
    • writePrefix

      protected void writePrefix(tools.jackson.core.JsonGenerator generator, Object object)
      Write a prefix before the main content.
      Parameters:
      generator - the generator to use for writing content.
      object - the object to write to the output message
    • writeSuffix

      protected void writeSuffix(tools.jackson.core.JsonGenerator generator, Object object)
      Write a suffix after the main content.
      Parameters:
      generator - the generator to use for writing content.
      object - the object to write to the output message
    • getJavaType

      protected tools.jackson.databind.JavaType getJavaType(Type type, @Nullable Class<?> contextClass)
      Return the Jackson JavaType for the specified type and context class.
      Parameters:
      type - the generic type to return the Jackson JavaType for
      contextClass - a context class for the target type, for example a class in which the target type appears in a method signature (can be null)
      Returns:
      the Jackson JavaType
    • getJsonEncoding

      protected tools.jackson.core.JsonEncoding getJsonEncoding(@Nullable MediaType contentType)
      Determine the JSON encoding to use for the given content type.
      Parameters:
      contentType - the media type as requested by the caller
      Returns:
      the JSON encoding to use (never null)
    • supportsRepeatableWrites

      protected boolean supportsRepeatableWrites(Object o)
      Description copied from class: AbstractHttpMessageConverter
      Indicates whether this message converter can write the given object multiple times.

      The default implementation returns false.

      Overrides:
      supportsRepeatableWrites in class AbstractHttpMessageConverter<Object>
      Parameters:
      o - the object t
      Returns:
      true if t can be written repeatedly; false otherwise