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_INCLUSIONis disabled
- DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIESis disabled
- Since:
- 4.0
- Author:
- Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze
- 
Field SummaryFields inherited from class org.springframework.messaging.converter.AbstractMessageConverterlogger
- 
Constructor SummaryConstructorsConstructorDescriptionConstruct aMappingJackson2MessageConverterwith a defaultObjectMapper, supporting theapplication/jsonMIME type withUTF-8character set.MappingJackson2MessageConverter(ObjectMapper objectMapper) Construct aMappingJackson2MessageConverterwith a customObjectMapper, supporting theapplication/jsonMIME type withUTF-8character set.MappingJackson2MessageConverter(ObjectMapper objectMapper, MimeType... supportedMimeTypes) Construct aMappingJackson2MessageConverterwith a customObjectMapper, supporting one or more custom MIME types.MappingJackson2MessageConverter(MimeType... supportedMimeTypes) Construct aMappingJackson2MessageConverterwith a defaultObjectMapper, supporting one or more custom MIME types.
- 
Method SummaryModifier and TypeMethodDescriptionprotected booleancanConvertFrom(Message<?> message, Class<?> targetClass) protected booleancanConvertTo(Object payload, MessageHeaders headers) protected ObjectconvertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) Convert the message payload from serialized form to an Object.protected ObjectconvertToInternal(Object payload, MessageHeaders headers, Object conversionHint) Convert the payload object to serialized form.protected JsonEncodinggetJsonEncoding(MimeType contentType) Determine the JSON encoding to use for the given content type.Return the underlyingObjectMapperfor this converter.protected Class<?>getSerializationView(Object conversionHint) Determine a Jackson serialization view based on the given conversion hint.protected voidlogWarningIfNecessary(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.voidsetObjectMapper(ObjectMapper objectMapper) Set theObjectMapperfor this converter.voidsetPrettyPrint(boolean prettyPrint) Whether to use theDefaultPrettyPrinterwhen writing JSON.protected booleanWhether the given class is supported by this converter.Methods inherited from class org.springframework.messaging.converter.AbstractMessageConverteraddSupportedMimeTypes, fromMessage, fromMessage, getContentTypeResolver, getDefaultContentType, getMimeType, getSerializedPayloadClass, getSupportedMimeTypes, isStrictContentTypeMatch, setContentTypeResolver, setSerializedPayloadClass, setStrictContentTypeMatch, supportsMimeType, toMessage, toMessage
- 
Constructor Details- 
MappingJackson2MessageConverterpublic MappingJackson2MessageConverter()Construct aMappingJackson2MessageConverterwith a defaultObjectMapper, supporting theapplication/jsonMIME type withUTF-8character set.
- 
MappingJackson2MessageConverterConstruct aMappingJackson2MessageConverterwith a defaultObjectMapper, supporting one or more custom MIME types.- Parameters:
- supportedMimeTypes- the supported MIME types
- Since:
- 4.1.5
 
- 
MappingJackson2MessageConverterConstruct aMappingJackson2MessageConverterwith a customObjectMapper, supporting theapplication/jsonMIME type withUTF-8character set.- Parameters:
- objectMapper- the- ObjectMapperto use
- Since:
- 6.1
 
- 
MappingJackson2MessageConverterConstruct aMappingJackson2MessageConverterwith a customObjectMapper, supporting one or more custom MIME types.- Parameters:
- objectMapper- the- ObjectMapperto use
- supportedMimeTypes- the supported MIME types
- Since:
- 6.1
 
 
- 
- 
Method Details- 
setObjectMapperSet theObjectMapperfor this converter.If not set, a default ObjectMapperis used.Setting a custom-configured ObjectMapperis one way to take further control of the JSON serialization process. For example, an extendedSerializerFactorycan 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-configuredObjectMapperis unnecessary.
- 
getObjectMapperReturn the underlyingObjectMapperfor this converter.
- 
setPrettyPrintpublic void setPrettyPrint(boolean prettyPrint) Whether to use theDefaultPrettyPrinterwhen writing JSON.This is a shortcut for setting up an ObjectMapperas follows:ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); converter.setObjectMapper(mapper); 
- 
canConvertFrom- Overrides:
- canConvertFromin class- AbstractMessageConverter
 
- 
canConvertTo- Overrides:
- canConvertToin class- AbstractMessageConverter
 
- 
logWarningIfNecessaryDetermine 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-)serializability
- cause- the Jackson-thrown exception to evaluate (typically a- JsonMappingException)
- Since:
- 4.3
 
- 
supportsDescription copied from class:AbstractMessageConverterWhether the given class is supported by this converter.- Specified by:
- supportsin class- AbstractMessageConverter
- Parameters:
- clazz- the class to test for support
- Returns:
- trueif supported;- falseotherwise
 
- 
convertFromInternal@Nullable protected Object convertFromInternal(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) Description copied from class:AbstractMessageConverterConvert the message payload from serialized form to an Object.- Overrides:
- convertFromInternalin class- AbstractMessageConverter
- Parameters:
- message- the input message
- targetClass- the target class for the conversion
- conversionHint- an extra object passed to the- MessageConverter, for example, the associated- MethodParameter(may be- null)
- Returns:
- the result of the conversion, or nullif the converter cannot perform the conversion
 
- 
convertToInternal@Nullable protected Object convertToInternal(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) Description copied from class:AbstractMessageConverterConvert the payload object to serialized form.- Overrides:
- convertToInternalin class- AbstractMessageConverter
- Parameters:
- payload- the Object to convert
- headers- optional headers for the message (may be- null)
- conversionHint- an extra object passed to the- MessageConverter, for example, the associated- MethodParameter(may be- null)
- Returns:
- the resulting payload for the message, or nullif the converter cannot perform the conversion
 
- 
getSerializationViewDetermine 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 nullif none
- Since:
- 4.2
 
- 
getJsonEncodingDetermine 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)
 
 
-