Class MappingJackson2MessageConverter
java.lang.Object
org.springframework.messaging.converter.AbstractMessageConverter
org.springframework.messaging.converter.MappingJackson2MessageConverter
- All Implemented Interfaces:
MessageConverter
,SmartMessageConverter
A Jackson 2 based
MessageConverter
implementation.
It customizes Jackson's default properties with the following ones:
MapperFeature.DEFAULT_VIEW_INCLUSION
is disabledDeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
is disabled
- Since:
- 4.0
- Author:
- Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze
-
Field Summary
Fields inherited from class org.springframework.messaging.converter.AbstractMessageConverter
logger
-
Constructor Summary
ConstructorDescriptionConstruct aMappingJackson2MessageConverter
with a defaultObjectMapper
, supporting theapplication/json
MIME type withUTF-8
character set.MappingJackson2MessageConverter
(ObjectMapper objectMapper) Construct aMappingJackson2MessageConverter
with a customObjectMapper
, supporting theapplication/json
MIME type withUTF-8
character set.MappingJackson2MessageConverter
(ObjectMapper objectMapper, MimeType... supportedMimeTypes) Construct aMappingJackson2MessageConverter
with a customObjectMapper
, supporting one or more custom MIME types.MappingJackson2MessageConverter
(MimeType... supportedMimeTypes) Construct aMappingJackson2MessageConverter
with a defaultObjectMapper
, supporting one or more custom MIME types. -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
canConvertFrom
(Message<?> message, Class<?> targetClass) protected boolean
canConvertTo
(Object payload, MessageHeaders headers) protected Object
convertFromInternal
(Message<?> message, Class<?> targetClass, Object conversionHint) Convert the message payload from serialized form to an Object.protected Object
convertToInternal
(Object payload, MessageHeaders headers, 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.Return the underlyingObjectMapper
for this converter.protected Class<?>
getSerializationView
(Object conversionHint) Determine a Jackson serialization view based on the given conversion hint.protected void
logWarningIfNecessary
(Type type, Throwable cause) Determine whether to log the given exception coming from aObjectMapper.canDeserialize(com.fasterxml.jackson.databind.JavaType)
/ObjectMapper.canSerialize(java.lang.Class<?>)
check.void
setObjectMapper
(ObjectMapper objectMapper) Set theObjectMapper
for this converter.void
setPrettyPrint
(boolean prettyPrint) Whether to use theDefaultPrettyPrinter
when writing JSON.protected boolean
Whether the given class is supported by this converter.Methods inherited from class org.springframework.messaging.converter.AbstractMessageConverter
addSupportedMimeTypes, fromMessage, fromMessage, getContentTypeResolver, getDefaultContentType, getMimeType, getSerializedPayloadClass, getSupportedMimeTypes, isStrictContentTypeMatch, setContentTypeResolver, setSerializedPayloadClass, setStrictContentTypeMatch, supportsMimeType, toMessage, toMessage
-
Constructor Details
-
MappingJackson2MessageConverter
public MappingJackson2MessageConverter()Construct aMappingJackson2MessageConverter
with a defaultObjectMapper
, supporting theapplication/json
MIME type withUTF-8
character set. -
MappingJackson2MessageConverter
Construct aMappingJackson2MessageConverter
with a defaultObjectMapper
, supporting one or more custom MIME types.- Parameters:
supportedMimeTypes
- the supported MIME types- Since:
- 4.1.5
-
MappingJackson2MessageConverter
Construct aMappingJackson2MessageConverter
with a customObjectMapper
, supporting theapplication/json
MIME type withUTF-8
character set.- Parameters:
objectMapper
- theObjectMapper
to use- Since:
- 6.1
-
MappingJackson2MessageConverter
Construct aMappingJackson2MessageConverter
with a customObjectMapper
, supporting one or more custom MIME types.- Parameters:
objectMapper
- theObjectMapper
to usesupportedMimeTypes
- the supported MIME types- Since:
- 6.1
-
-
Method Details
-
setObjectMapper
Set theObjectMapper
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 extendedSerializerFactory
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-configuredObjectMapper
is unnecessary. -
getObjectMapper
Return the underlyingObjectMapper
for this converter. -
setPrettyPrint
public void setPrettyPrint(boolean prettyPrint) Whether to use theDefaultPrettyPrinter
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);
-
canConvertFrom
- Overrides:
canConvertFrom
in classAbstractMessageConverter
-
canConvertTo
- Overrides:
canConvertTo
in classAbstractMessageConverter
-
logWarningIfNecessary
Determine whether to log the given exception coming from aObjectMapper.canDeserialize(com.fasterxml.jackson.databind.JavaType)
/ObjectMapper.canSerialize(java.lang.Class<?>)
check.- Parameters:
type
- the class that Jackson tested for (de-)serializabilitycause
- the Jackson-thrown exception to evaluate (typically aJsonMappingException
)- Since:
- 4.3
-
supports
Description copied from class:AbstractMessageConverter
Whether the given class is supported by this converter.- Specified by:
supports
in classAbstractMessageConverter
- Parameters:
clazz
- the class to test for support- Returns:
true
if supported;false
otherwise
-
convertFromInternal
@Nullable protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) Description copied from class:AbstractMessageConverter
Convert the message payload from serialized form to an Object.- Overrides:
convertFromInternal
in classAbstractMessageConverter
- Parameters:
message
- the input messagetargetClass
- the target class for the conversionconversionHint
- an extra object passed to theMessageConverter
, for example, the associatedMethodParameter
(may benull
}- Returns:
- the result of the conversion, or
null
if the converter cannot perform the conversion
-
convertToInternal
@Nullable protected Object convertToInternal(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) Description copied from class:AbstractMessageConverter
Convert the payload object to serialized form.- Overrides:
convertToInternal
in classAbstractMessageConverter
- Parameters:
payload
- the Object to convertheaders
- optional headers for the message (may benull
)conversionHint
- an extra object passed to theMessageConverter
, for example, the associatedMethodParameter
(may benull
}- Returns:
- the resulting payload for the message, or
null
if the converter cannot perform the conversion
-
getSerializationView
Determine a Jackson serialization view based on the given conversion hint.- Parameters:
conversionHint
- the conversion hint Object as passed into the converter for the current conversion attempt- Returns:
- the serialization view class, or
null
if none - Since:
- 4.2
-
getJsonEncoding
Determine the JSON encoding to use for the given content type.- Parameters:
contentType
- the MIME type from the MessageHeaders, if any- Returns:
- the JSON encoding to use (never
null
)
-