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.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 Summary
FieldsFields inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
logger
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
AbstractJacksonHttpMessageConverter
(MapperBuilder<?, ?> builder, MediaType supportedMediaType) Construct a new instance with the providedbuilder
customized with theJacksonModule
s found byMapperBuilder.findModules(ClassLoader)
andMediaType
.protected
AbstractJacksonHttpMessageConverter
(MapperBuilder<?, ?> builder, MediaType... supportedMediaTypes) Construct a new instance with the providedbuilder
customized with theJacksonModule
s found byMapperBuilder.findModules(ClassLoader)
andMediaType
s.protected
AbstractJacksonHttpMessageConverter
(ObjectMapper objectMapper) Construct a new instance with the providedObjectMapper
.protected
AbstractJacksonHttpMessageConverter
(ObjectMapper objectMapper, MediaType supportedMediaType) Construct a new instance with the providedObjectMapper
andMediaType
.protected
AbstractJacksonHttpMessageConverter
(ObjectMapper objectMapper, MediaType... supportedMediaTypes) Construct a new instance with the providedObjectMapper
andMediaType
s. -
Method Summary
Modifier and TypeMethodDescriptionboolean
canRead
(ResolvableType type, @Nullable MediaType mediaType) Indicates whether the given type can be read by this converter.boolean
protected ObjectReader
customizeReader
(ObjectReader reader, JavaType javaType) Subclasses can use this method to customize theObjectReader
used for reading values.protected ObjectWriter
customizeWriter
(ObjectWriter writer, @Nullable JavaType javaType, @Nullable MediaType contentType) Subclasses can use this method to customize theObjectWriter
used for writing values.protected Charset
getCharset
(@Nullable MediaType contentType) Determine the charset to use for JSON input.protected JavaType
getJavaType
(Type type, @Nullable Class<?> contextClass) Return the JacksonJavaType
for the specified type and context class.protected tools.jackson.core.JsonEncoding
getJsonEncoding
(@Nullable MediaType contentType) Determine the JSON encoding to use for the given content type.Return the supported media type(s) forProblemDetail
.Return the mainObjectMapper
in 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 Object
readInternal
(Class<?> clazz, HttpInputMessage inputMessage) Abstract template method that reads the actual object.void
registerObjectMappersForType
(Class<?> clazz, Consumer<Map<MediaType, ObjectMapper>> registrar) Configure theObjectMapper
instances to use for the givenClass
.void
setSupportedMediaTypes
(List<MediaType> supportedMediaTypes) Set the list ofMediaType
objects supported by this converter.protected boolean
Indicates whether this message converter can write the given object multiple times.protected void
writeInternal
(Object object, ResolvableType resolvableType, HttpOutputMessage outputMessage, @Nullable Map<String, Object> hints) Abstract template method that writes the actual body.protected void
writePrefix
(tools.jackson.core.JsonGenerator generator, Object object) Write a prefix before the main content.protected void
writeSuffix
(tools.jackson.core.JsonGenerator generator, Object object) Write a suffix after the main content.Methods inherited from class org.springframework.http.converter.AbstractSmartHttpMessageConverter
canWrite, supports, write, writeInternal
Methods inherited from class org.springframework.http.converter.AbstractHttpMessageConverter
addDefaultHeaders, canRead, canRead, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, write
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.http.converter.HttpMessageConverter
getSupportedMediaTypes
Methods inherited from interface org.springframework.http.converter.SmartHttpMessageConverter
canRead, read, write
-
Field Details
-
defaultObjectMapper
-
-
Constructor Details
-
AbstractJacksonHttpMessageConverter
protected AbstractJacksonHttpMessageConverter(MapperBuilder<?, ?> builder, MediaType supportedMediaType) Construct a new instance with the providedbuilder
customized with theJacksonModule
s found byMapperBuilder.findModules(ClassLoader)
andMediaType
. -
AbstractJacksonHttpMessageConverter
protected AbstractJacksonHttpMessageConverter(MapperBuilder<?, ?> builder, MediaType... supportedMediaTypes) Construct a new instance with the providedbuilder
customized with theJacksonModule
s found byMapperBuilder.findModules(ClassLoader)
andMediaType
s. -
AbstractJacksonHttpMessageConverter
Construct a new instance with the providedObjectMapper
. -
AbstractJacksonHttpMessageConverter
protected AbstractJacksonHttpMessageConverter(ObjectMapper objectMapper, MediaType supportedMediaType) Construct a new instance with the providedObjectMapper
andMediaType
. -
AbstractJacksonHttpMessageConverter
protected AbstractJacksonHttpMessageConverter(ObjectMapper objectMapper, MediaType... supportedMediaTypes) Construct a new instance with the providedObjectMapper
andMediaType
s.
-
-
Method Details
-
setSupportedMediaTypes
Description copied from class:AbstractHttpMessageConverter
Set the list ofMediaType
objects supported by this converter.- Overrides:
setSupportedMediaTypes
in classAbstractHttpMessageConverter<Object>
-
getObjectMapper
Return the mainObjectMapper
in use. -
registerObjectMappersForType
public void registerObjectMappersForType(Class<?> clazz, Consumer<Map<MediaType, ObjectMapper>> registrar) Configure theObjectMapper
instances to use for the givenClass
. This is useful when you want to deviate from thedefault
ObjectMapper or have theObjectMapper
vary byMediaType
.Note: Use of this method effectively turns off use of the default
ObjectMapper
andsupportedMediaTypes
for the given class. Therefore it is important for the mappings configured here toinclude
every MediaType that must be supported for the given class.- Parameters:
clazz
- the type of Object to register ObjectMapper instances forregistrar
- a consumer to populate or otherwise update the MediaType-to-ObjectMapper associations for the given Class
-
getObjectMappersForType
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
Description copied from interface:HttpMessageConverter
Return 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
-
getMediaTypesForProblemDetail
Return the supported media type(s) forProblemDetail
. By default, an empty list, unless overridden in subclasses. -
canRead
Description copied from interface:SmartHttpMessageConverter
Indicates 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:
canRead
in interfaceSmartHttpMessageConverter<Object>
- Overrides:
canRead
in classAbstractSmartHttpMessageConverter<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 benull
if not specified. Typically, the value of aContent-Type
header.- Returns:
true
if readable;false
otherwise
-
canWrite
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 interfaceHttpMessageConverter<Object>
- Specified by:
canWrite
in interfaceSmartHttpMessageConverter<Object>
- Overrides:
canWrite
in classAbstractHttpMessageConverter<Object>
- Parameters:
clazz
- the class to test for writabilitymediaType
- the media type to write (can benull
if not specified); typically the value of anAccept
header.- Returns:
true
if writable;false
otherwise
-
read
public Object read(ResolvableType type, HttpInputMessage inputMessage, @Nullable Map<String, Object> hints) throws IOException, HttpMessageNotReadableExceptionDescription 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 thecanRead
method of this interface, which must have returnedtrue
. 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 fromhints
- additional information about how to encode- Returns:
- the converted object
- Throws:
IOException
- in case of I/O errorsHttpMessageNotReadableException
- 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 fromAbstractHttpMessageConverter.read(java.lang.Class<? extends T>, org.springframework.http.HttpInputMessage)
.- Overrides:
readInternal
in classAbstractSmartHttpMessageConverter<Object>
- Parameters:
clazz
- the type of object to returninputMessage
- the HTTP input message to read from- Returns:
- the converted object
- Throws:
IOException
- in case of I/O errorsHttpMessageNotReadableException
- in case of conversion errors
-
customizeReader
Subclasses can use this method to customize theObjectReader
used for reading values.- Parameters:
reader
- the reader instance to customizejavaType
- the type of element values to read- Returns:
- the customized
ObjectReader
-
getCharset
Determine the charset to use for JSON input.By default this is either the charset from the input
MediaType
or otherwiseUTF-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, HttpMessageNotWritableExceptionDescription copied from class:AbstractSmartHttpMessageConverter
Abstract template method that writes the actual body. Invoked fromAbstractSmartHttpMessageConverter.write(Object, ResolvableType, MediaType, HttpOutputMessage, Map)
.- Specified by:
writeInternal
in classAbstractSmartHttpMessageConverter<Object>
- Parameters:
object
- the object to write to the output messageresolvableType
- the type of object to writeoutputMessage
- the HTTP output message to write tohints
- additional information about how to encode- Throws:
IOException
- in case of I/O errorsHttpMessageNotWritableException
- in case of conversion errors
-
customizeWriter
protected ObjectWriter customizeWriter(ObjectWriter writer, @Nullable JavaType javaType, @Nullable MediaType contentType) Subclasses can use this method to customize theObjectWriter
used for writing values.- Parameters:
writer
- the writer instance to customizejavaType
- the type of element values to writecontentType
- the selected media type- Returns:
- the customized
ObjectWriter
-
writePrefix
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
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
Return the JacksonJavaType
for the specified type and context class.- Parameters:
type
- the generic type to return the Jackson JavaType forcontextClass
- a context class for the target type, for example a class in which the target type appears in a method signature (can benull
)- Returns:
- the Jackson JavaType
-
getJsonEncoding
-
supportsRepeatableWrites
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 classAbstractHttpMessageConverter<Object>
- Parameters:
o
- the object t- Returns:
true
ift
can be written repeatedly;false
otherwise
-