Class AbstractJacksonHttpMessageConverter
java.lang.Object
org.springframework.http.converter.AbstractHttpMessageConverter<Object>
org.springframework.http.converter.AbstractSmartHttpMessageConverter<Object>
org.springframework.http.converter.AbstractJacksonHttpMessageConverter
- All Implemented Interfaces:
- HttpMessageConverter<Object>,- SmartHttpMessageConverter<Object>
- Direct Known Subclasses:
- JacksonCborHttpMessageConverter,- JacksonJsonHttpMessageConverter,- JacksonSmileHttpMessageConverter,- JacksonXmlHttpMessageConverter,- JacksonYamlHttpMessageConverter
public abstract class AbstractJacksonHttpMessageConverter
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.JsonViewkey and the class name of the JSON view as value.
- A filter provider with a tools.jackson.databind.ser.FilterProviderkey and the filter provider class name as value.
- Since:
- 7.0
- Author:
- Sebastien Deleuze
- See Also:
- 
Field SummaryFieldsFields inherited from class org.springframework.http.converter.AbstractHttpMessageConverterlogger
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedAbstractJacksonHttpMessageConverter(MapperBuilder<?, ?> builder, MediaType supportedMediaType) Construct a new instance with the providedbuildercustomized with theJacksonModules found byMapperBuilder.findModules(ClassLoader)andMediaType.protectedAbstractJacksonHttpMessageConverter(MapperBuilder<?, ?> builder, MediaType... supportedMediaTypes) Construct a new instance with the providedbuildercustomized with theJacksonModules found byMapperBuilder.findModules(ClassLoader)andMediaTypes.protectedAbstractJacksonHttpMessageConverter(ObjectMapper objectMapper) Construct a new instance with the providedObjectMapper.protectedAbstractJacksonHttpMessageConverter(ObjectMapper objectMapper, MediaType supportedMediaType) Construct a new instance with the providedObjectMapperandMediaType.protectedAbstractJacksonHttpMessageConverter(ObjectMapper objectMapper, MediaType... supportedMediaTypes) Construct a new instance with the providedObjectMapperandMediaTypes.
- 
Method SummaryModifier and TypeMethodDescriptionbooleancanRead(ResolvableType type, @Nullable MediaType mediaType) Indicates whether the given type can be read by this converter.booleanprotected ObjectReadercustomizeReader(ObjectReader reader, JavaType javaType) Subclasses can use this method to customize theObjectReaderused for reading values.protected ObjectWritercustomizeWriter(ObjectWriter writer, @Nullable JavaType javaType, @Nullable MediaType contentType) Subclasses can use this method to customize theObjectWriterused for writing values.protected CharsetgetCharset(@Nullable MediaType contentType) Determine the charset to use for JSON input.protected JavaTypegetJavaType(Type type, @Nullable Class<?> contextClass) Return the JacksonJavaTypefor the specified type and context class.protected tools.jackson.core.JsonEncodinggetJsonEncoding(@Nullable MediaType contentType) Determine the JSON encoding to use for the given content type.Return the supported media type(s) forProblemDetail.Return the mainObjectMapperin use.getObjectMappersForType(Class<?> clazz) Return ObjectMapper registrations for the given class, if any.getSupportedMediaTypes(Class<?> clazz) Return the list of media types supported by this converter for the given class.read(ResolvableType type, HttpInputMessage inputMessage, @Nullable Map<String, Object> hints) Read an object of the given type from the given input message, and returns it.protected ObjectreadInternal(Class<?> clazz, HttpInputMessage inputMessage) Abstract template method that reads the actual object.voidregisterObjectMappersForType(Class<?> clazz, Consumer<Map<MediaType, ObjectMapper>> registrar) Configure theObjectMapperinstances to use for the givenClass.voidsetSupportedMediaTypes(List<MediaType> supportedMediaTypes) Set the list ofMediaTypeobjects supported by this converter.protected booleanIndicates whether this message converter can write the given object multiple times.protected voidwriteInternal(Object object, ResolvableType resolvableType, HttpOutputMessage outputMessage, @Nullable Map<String, Object> hints) Abstract template method that writes the actual body.protected voidwritePrefix(tools.jackson.core.JsonGenerator generator, Object object) Write a prefix before the main content.protected voidwriteSuffix(tools.jackson.core.JsonGenerator generator, Object object) Write a suffix after the main content.Methods inherited from class org.springframework.http.converter.AbstractSmartHttpMessageConvertercanWrite, supports, write, writeInternalMethods inherited from class org.springframework.http.converter.AbstractHttpMessageConverteraddDefaultHeaders, canRead, canRead, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, writeMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.http.converter.HttpMessageConvertergetSupportedMediaTypesMethods inherited from interface org.springframework.http.converter.SmartHttpMessageConvertercanRead, read, write
- 
Field Details- 
defaultObjectMapper
 
- 
- 
Constructor Details- 
AbstractJacksonHttpMessageConverterprotected AbstractJacksonHttpMessageConverter(MapperBuilder<?, ?> builder, MediaType supportedMediaType) Construct a new instance with the providedbuildercustomized with theJacksonModules found byMapperBuilder.findModules(ClassLoader)andMediaType.
- 
AbstractJacksonHttpMessageConverterprotected AbstractJacksonHttpMessageConverter(MapperBuilder<?, ?> builder, MediaType... supportedMediaTypes) Construct a new instance with the providedbuildercustomized with theJacksonModules found byMapperBuilder.findModules(ClassLoader)andMediaTypes.
- 
AbstractJacksonHttpMessageConverterConstruct a new instance with the providedObjectMapper.
- 
AbstractJacksonHttpMessageConverterprotected AbstractJacksonHttpMessageConverter(ObjectMapper objectMapper, MediaType supportedMediaType) Construct a new instance with the providedObjectMapperandMediaType.
- 
AbstractJacksonHttpMessageConverterprotected AbstractJacksonHttpMessageConverter(ObjectMapper objectMapper, MediaType... supportedMediaTypes) Construct a new instance with the providedObjectMapperandMediaTypes.
 
- 
- 
Method Details- 
setSupportedMediaTypesDescription copied from class:AbstractHttpMessageConverterSet the list ofMediaTypeobjects supported by this converter.- Overrides:
- setSupportedMediaTypesin class- AbstractHttpMessageConverter<Object>
 
- 
getObjectMapperReturn the mainObjectMapperin use.
- 
registerObjectMappersForTypepublic void registerObjectMappersForType(Class<?> clazz, Consumer<Map<MediaType, ObjectMapper>> registrar) Configure theObjectMapperinstances to use for the givenClass. This is useful when you want to deviate from thedefaultObjectMapper or have theObjectMappervary byMediaType.Note: Use of this method effectively turns off use of the default ObjectMapperandsupportedMediaTypesfor the given class. Therefore it is important for the mappings configured here toincludeevery 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
 
- 
getObjectMappersForTypeReturn 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.
 
- 
getSupportedMediaTypesDescription copied from interface:HttpMessageConverterReturn the list of media types supported by this converter for the given class. The list may differ fromHttpMessageConverter.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
 
- 
getMediaTypesForProblemDetailReturn the supported media type(s) forProblemDetail. By default, an empty list, unless overridden in subclasses.
- 
canReadDescription copied from interface:SmartHttpMessageConverterIndicates whether the given type can be read by this converter. This method should perform the same checks asHttpMessageConverter.canRead(Class, MediaType)with additional ones related to the generic type.- Specified by:
- canReadin interface- SmartHttpMessageConverter<Object>
- Overrides:
- canReadin 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- nullif not specified. Typically, the value of a- Content-Typeheader.
- Returns:
- trueif readable;- falseotherwise
 
- 
canWriteDescription copied from class:AbstractHttpMessageConverterThis implementation checks if the given class is supported, and if the supported media types include the given media type.- Specified by:
- canWritein interface- HttpMessageConverter<Object>
- Specified by:
- canWritein interface- SmartHttpMessageConverter<Object>
- Overrides:
- canWritein class- AbstractHttpMessageConverter<Object>
- Parameters:
- clazz- the class to test for writability
- mediaType- the media type to write (can be- nullif not specified); typically the value of an- Acceptheader.
- Returns:
- trueif writable;- falseotherwise
 
- 
readpublic Object read(ResolvableType type, HttpInputMessage inputMessage, @Nullable Map<String, Object> hints) throws IOException, HttpMessageNotReadableExceptionDescription copied from interface:SmartHttpMessageConverterRead 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- canReadmethod 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
 
- 
readInternalprotected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException Description copied from class:AbstractHttpMessageConverterAbstract template method that reads the actual object. Invoked fromAbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage).- Overrides:
- readInternalin 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
 
- 
customizeReaderSubclasses can use this method to customize theObjectReaderused for reading values.- Parameters:
- reader- the reader instance to customize
- javaType- the type of element values to read
- Returns:
- the customized ObjectReader
 
- 
getCharsetDetermine the charset to use for JSON input.By default this is either the charset from the input MediaTypeor otherwiseUTF-8. Can be overridden in subclasses.- Parameters:
- contentType- the content type of the HTTP input message
- Returns:
- the charset to use
 
- 
writeInternalprotected void writeInternal(Object object, ResolvableType resolvableType, HttpOutputMessage outputMessage, @Nullable Map<String, Object> hints) throws IOException, HttpMessageNotWritableExceptionDescription copied from class:AbstractSmartHttpMessageConverterAbstract template method that writes the actual body. Invoked fromAbstractSmartHttpMessageConverter.write(Object, ResolvableType, MediaType, HttpOutputMessage, Map).- Specified by:
- writeInternalin 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
 
- 
customizeWriterprotected ObjectWriter customizeWriter(ObjectWriter writer, @Nullable JavaType javaType, @Nullable MediaType contentType) Subclasses can use this method to customize theObjectWriterused 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
 
- 
writePrefixWrite a prefix before the main content.- Parameters:
- generator- the generator to use for writing content.
- object- the object to write to the output message
 
- 
writeSuffixWrite a suffix after the main content.- Parameters:
- generator- the generator to use for writing content.
- object- the object to write to the output message
 
- 
getJavaTypeReturn the JacksonJavaTypefor 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
- 
supportsRepeatableWritesDescription copied from class:AbstractHttpMessageConverterIndicates whether this message converter can write the given object multiple times.The default implementation returns false.- Overrides:
- supportsRepeatableWritesin class- AbstractHttpMessageConverter<Object>
- Parameters:
- o- the object t
- Returns:
- trueif- tcan be written repeatedly;- falseotherwise
 
 
-