Class AbstractJacksonHttpMessageConverter<T extends tools.jackson.databind.ObjectMapper>
java.lang.Object
org.springframework.http.converter.AbstractHttpMessageConverter<Object>
org.springframework.http.converter.AbstractSmartHttpMessageConverter<Object>
org.springframework.http.converter.AbstractJacksonHttpMessageConverter<T>
- Type Parameters:
T- the type ofObjectMapper
- 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.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 Summary
FieldsFields inherited from class AbstractHttpMessageConverter
logger -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractJacksonHttpMessageConverter(T mapper) Construct a new instance with the providedObjectMapper.protectedAbstractJacksonHttpMessageConverter(tools.jackson.databind.cfg.MapperBuilder<T, ?> builder, MediaType supportedMediaType) Construct a new instance with the providedbuildercustomized with theJacksonModules found byMapperBuilder.findModules(ClassLoader)andMediaType.protectedAbstractJacksonHttpMessageConverter(tools.jackson.databind.cfg.MapperBuilder<T, ?> builder, MediaType... supportedMediaTypes) Construct a new instance with the providedbuildercustomized with theJacksonModules found byMapperBuilder.findModules(ClassLoader)andMediaTypes.protectedAbstractJacksonHttpMessageConverter(T mapper, MediaType supportedMediaType) Construct a new instance with the providedObjectMapperandMediaType.protectedAbstractJacksonHttpMessageConverter(T mapper, MediaType... supportedMediaTypes) Construct a new instance with the providedObjectMapperandMediaTypes. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanRead(ResolvableType type, @Nullable MediaType mediaType) Indicates whether the given type can be read by this converter.booleanprotected tools.jackson.databind.ObjectReadercustomizeReader(tools.jackson.databind.ObjectReader reader, tools.jackson.databind.JavaType javaType) Subclasses can use this method to customize theObjectReaderused for reading values.protected tools.jackson.databind.ObjectWritercustomizeWriter(tools.jackson.databind.ObjectWriter writer, @Nullable tools.jackson.databind.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 tools.jackson.databind.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 mainObjectMapperin use.getMappersForType(Class<?> clazz) Return ObjectMapper registrations for the given class, if any.Return the supported media type(s) forProblemDetail.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.voidConfigure 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 AbstractSmartHttpMessageConverter
canWrite, supports, write, writeInternalMethods inherited from class AbstractHttpMessageConverter
addDefaultHeaders, canRead, canRead, canWrite, getContentLength, getDefaultCharset, getDefaultContentType, getSupportedMediaTypes, read, setDefaultCharset, writeMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface HttpMessageConverter
getSupportedMediaTypesMethods inherited from interface SmartHttpMessageConverter
canRead, read, write
-
Field Details
-
defaultMapper
-
-
Constructor Details
-
AbstractJacksonHttpMessageConverter
-
AbstractJacksonHttpMessageConverter
-
AbstractJacksonHttpMessageConverter
Construct a new instance with the providedObjectMapper. -
AbstractJacksonHttpMessageConverter
-
AbstractJacksonHttpMessageConverter
-
-
Method Details
-
setSupportedMediaTypes
Description copied from class:AbstractHttpMessageConverterSet the list ofMediaTypeobjects supported by this converter.- Overrides:
setSupportedMediaTypesin classAbstractHttpMessageConverter<Object>
-
getMapper
Return the mainObjectMapperin use. -
registerMappersForType
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 forregistrar- a consumer to populate or otherwise update the MediaType-to-ObjectMapper associations for the given Class
-
getMappersForType
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: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
-
getMediaTypesForProblemDetail
Return the supported media type(s) forProblemDetail. By default, an empty list, unless overridden in subclasses. -
canRead
Description 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 interfaceSmartHttpMessageConverter<T extends tools.jackson.databind.ObjectMapper>- Overrides:
canReadin 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 benullif not specified. Typically, the value of aContent-Typeheader.- Returns:
trueif readable;falseotherwise
-
canWrite
Description 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 interfaceHttpMessageConverter<T extends tools.jackson.databind.ObjectMapper>- Specified by:
canWritein interfaceSmartHttpMessageConverter<T extends tools.jackson.databind.ObjectMapper>- Overrides:
canWritein classAbstractHttpMessageConverter<Object>- Parameters:
clazz- the class to test for writabilitymediaType- the media type to write (can benullif not specified); typically the value of anAcceptheader.- Returns:
trueif writable;falseotherwise
-
read
public 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 thecanReadmethod 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:AbstractHttpMessageConverterAbstract template method that reads the actual object. Invoked fromAbstractHttpMessageConverter.read(Class, HttpInputMessage).- Overrides:
readInternalin 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
protected tools.jackson.databind.ObjectReader customizeReader(tools.jackson.databind.ObjectReader reader, tools.jackson.databind.JavaType javaType) Subclasses can use this method to customize theObjectReaderused 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
MediaTypeor 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:AbstractSmartHttpMessageConverterAbstract template method that writes the actual body. Invoked fromAbstractSmartHttpMessageConverter.write(Object, ResolvableType, MediaType, HttpOutputMessage, Map).- Specified by:
writeInternalin 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 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 theObjectWriterused 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 JacksonJavaTypefor 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:AbstractHttpMessageConverterIndicates whether this message converter can write the given object multiple times.The default implementation returns
false.- Overrides:
supportsRepeatableWritesin classAbstractHttpMessageConverter<Object>- Parameters:
o- the object t- Returns:
trueiftcan be written repeatedly;falseotherwise
-