public class MappingJackson2MessageConverter extends AbstractMessageConverter
MessageConverter
implementation.
It customizes Jackson's default properties with the following ones:
MapperFeature.DEFAULT_VIEW_INCLUSION
is disabledDeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
is disabledCompatible with Jackson 2.9 and higher, as of Spring 5.1.
logger
Constructor and Description |
---|
MappingJackson2MessageConverter()
Construct a
MappingJackson2MessageConverter supporting
the application/json MIME type with UTF-8 character set. |
MappingJackson2MessageConverter(MimeType... supportedMimeTypes)
Construct a
MappingJackson2MessageConverter supporting
one or more custom MIME types. |
Modifier and Type | Method and Description |
---|---|
protected boolean |
canConvertFrom(Message<?> message,
java.lang.Class<?> targetClass) |
protected boolean |
canConvertTo(java.lang.Object payload,
MessageHeaders headers) |
protected java.lang.Object |
convertFromInternal(Message<?> message,
java.lang.Class<?> targetClass,
java.lang.Object conversionHint)
Convert the message payload from serialized form to an Object.
|
protected java.lang.Object |
convertToInternal(java.lang.Object payload,
MessageHeaders headers,
java.lang.Object conversionHint)
Convert the payload object to serialized form.
|
protected JsonEncoding |
getJsonEncoding(MimeType contentType)
Determine the JSON encoding to use for the given content type.
|
ObjectMapper |
getObjectMapper()
Return the underlying
ObjectMapper for this converter. |
protected java.lang.Class<?> |
getSerializationView(java.lang.Object conversionHint)
Determine a Jackson serialization view based on the given conversion hint.
|
protected void |
logWarningIfNecessary(java.lang.reflect.Type type,
java.lang.Throwable cause)
Determine whether to log the given exception coming from a
ObjectMapper.canDeserialize(com.fasterxml.jackson.databind.JavaType) / ObjectMapper.canSerialize(java.lang.Class<?>) check. |
void |
setObjectMapper(ObjectMapper objectMapper)
Set the
ObjectMapper for this converter. |
void |
setPrettyPrint(boolean prettyPrint)
Whether to use the
DefaultPrettyPrinter when writing JSON. |
protected boolean |
supports(java.lang.Class<?> clazz)
Whether the given class is supported by this converter.
|
fromMessage, fromMessage, getContentTypeResolver, getDefaultContentType, getMimeType, getSerializedPayloadClass, getSupportedMimeTypes, isStrictContentTypeMatch, setContentTypeResolver, setSerializedPayloadClass, setStrictContentTypeMatch, supportsMimeType, toMessage, toMessage
public MappingJackson2MessageConverter()
MappingJackson2MessageConverter
supporting
the application/json
MIME type with UTF-8
character set.public MappingJackson2MessageConverter(MimeType... supportedMimeTypes)
MappingJackson2MessageConverter
supporting
one or more custom MIME types.supportedMimeTypes
- the supported MIME typespublic void setObjectMapper(ObjectMapper objectMapper)
ObjectMapper
for this converter.
If not set, a default ObjectMapper
is used.
Setting a custom-configured ObjectMapper
is one way to take further
control of the JSON serialization process. For example, an extended
SerializerFactory
can be
configured that provides custom serializers for specific types. The other
option for refining the serialization process is to use Jackson's provided
annotations on the types to be serialized, in which case a custom-configured
ObjectMapper is unnecessary.
public ObjectMapper getObjectMapper()
ObjectMapper
for this converter.public void setPrettyPrint(boolean prettyPrint)
DefaultPrettyPrinter
when writing JSON.
This is a shortcut for setting up an ObjectMapper
as follows:
ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); converter.setObjectMapper(mapper);
protected boolean canConvertFrom(Message<?> message, @Nullable java.lang.Class<?> targetClass)
canConvertFrom
in class AbstractMessageConverter
protected boolean canConvertTo(java.lang.Object payload, @Nullable MessageHeaders headers)
canConvertTo
in class AbstractMessageConverter
protected void logWarningIfNecessary(java.lang.reflect.Type type, @Nullable java.lang.Throwable cause)
ObjectMapper.canDeserialize(com.fasterxml.jackson.databind.JavaType)
/ ObjectMapper.canSerialize(java.lang.Class<?>)
check.type
- the class that Jackson tested for (de-)serializabilitycause
- the Jackson-thrown exception to evaluate
(typically a JsonMappingException
)protected boolean supports(java.lang.Class<?> clazz)
AbstractMessageConverter
supports
in class AbstractMessageConverter
clazz
- the class to test for supporttrue
if supported; false
otherwise@Nullable protected java.lang.Object convertFromInternal(Message<?> message, java.lang.Class<?> targetClass, @Nullable java.lang.Object conversionHint)
AbstractMessageConverter
convertFromInternal
in class AbstractMessageConverter
message
- the input messagetargetClass
- the target class for the conversionconversionHint
- an extra object passed to the MessageConverter
,
e.g. the associated MethodParameter
(may be null
}null
if the converter cannot
perform the conversion@Nullable protected java.lang.Object convertToInternal(java.lang.Object payload, @Nullable MessageHeaders headers, @Nullable java.lang.Object conversionHint)
AbstractMessageConverter
convertToInternal
in class AbstractMessageConverter
payload
- the Object to convertheaders
- optional headers for the message (may be null
)conversionHint
- an extra object passed to the MessageConverter
,
e.g. the associated MethodParameter
(may be null
}null
if the converter
cannot perform the conversion@Nullable protected java.lang.Class<?> getSerializationView(@Nullable java.lang.Object conversionHint)
conversionHint
- the conversion hint Object as passed into the
converter for the current conversion attemptnull
if noneprotected JsonEncoding getJsonEncoding(@Nullable MimeType contentType)
contentType
- the MIME type from the MessageHeaders, if anynull
)