Top Description Fields Constructors Methods
org.springframework.messaging.converter

public Class MappingJackson2MessageConverter

extends AbstractMessageConverter
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .IOException, .StringWriter, .Writer, java.lang.reflect.Type, java.nio.charset.Charset, java.util.concurrent.atomic.AtomicReference, com.fasterxml.jackson.annotation.JsonView, com.fasterxml.jackson.core.JsonEncoding, .JsonGenerator, com.fasterxml.jackson.core.util.DefaultPrettyPrinter, com.fasterxml.jackson.databind.DeserializationFeature, .JavaType, .JsonMappingException, .MapperFeature, .ObjectMapper, .SerializationFeature, org.springframework.core.MethodParameter, org.springframework.lang.Nullable, org.springframework.messaging.Message, .MessageHeaders, org.springframework.util.Assert, .ClassUtils, .MimeType

A Jackson 2 based MessageConverter implementation.

It customizes Jackson's default properties with the following ones:

Authors
Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze
Since
4.0

Field Summary

Modifier and TypeField and Description
private static final MimeType[]
private ObjectMapper
private Boolean
Inherited from org.springframework.messaging.converter.AbstractMessageConverter:
logger

Constructor Summary

AccessConstructor and Description
public
MappingJackson2MessageConverter()

Construct a MappingJackson2MessageConverter with a default ObjectMapper, supporting the application/json MIME type with UTF-8 character set.

public
MappingJackson2MessageConverter(MimeType...
the supported MIME types
supportedMimeTypes
)

Construct a MappingJackson2MessageConverter with a default ObjectMapper, supporting one or more custom MIME types.

public
MappingJackson2MessageConverter(ObjectMapper
the ObjectMapper to use
objectMapper
)

Construct a MappingJackson2MessageConverter with a custom ObjectMapper, supporting the application/json MIME type with UTF-8 character set.

public
MappingJackson2MessageConverter(ObjectMapper
the ObjectMapper to use
objectMapper
,
MimeType...
the supported MIME types
supportedMimeTypes
)

Construct a MappingJackson2MessageConverter with a custom ObjectMapper, supporting one or more custom MIME types.

Method Summary

Modifier and TypeMethod and Description
protected boolean
protected boolean
private void
protected Object
convertFromInternal(Message<?>
the input message
message
,
Class<?>
the target class for the conversion
targetClass
,
Object
an extra object passed to the MessageConverter, e.g. the associated MethodParameter (may be null}
conversionHint
)

Overrides org.springframework.messaging.converter.AbstractMessageConverter.convertFromInternal.

Convert the message payload from serialized form to an Object.

protected Object
convertToInternal(Object
the Object to convert
payload
,
MessageHeaders
optional headers for the message (may be null)
headers
,
Object
an extra object passed to the MessageConverter, e.g. the associated MethodParameter (may be null}
conversionHint
)

Overrides org.springframework.messaging.converter.AbstractMessageConverter.convertToInternal.

Convert the payload object to serialized form.

private Class<?>
extractViewClass(JsonView annotation, Object conversionHint)

protected JsonEncoding

Returns:

the JSON encoding to use (never null)
getJsonEncoding
(MimeType
the MIME type from the MessageHeaders, if any
contentType
)

Determine the JSON encoding to use for the given content type.

public ObjectMapper
getObjectMapper()

Return the underlying ObjectMapper for this converter.

protected Class<?>

Returns:

the serialization view class, or null if none
getSerializationView
(Object
the conversion hint Object as passed into the converter for the current conversion attempt
conversionHint
)

Determine a Jackson serialization view based on the given conversion hint.

protected void
logWarningIfNecessary(Type
the class that Jackson tested for (de-)serializability
type
,
Throwable
the Jackson-thrown exception to evaluate (typically a JsonMappingException)
cause
)

Determine whether to log the given exception coming from a ObjectMapper#canDeserialize / ObjectMapper#canSerialize check.

public void
setObjectMapper(ObjectMapper objectMapper)

Set the ObjectMapper for this converter.

public void
setPrettyPrint(boolean prettyPrint)

Whether to use the DefaultPrettyPrinter when writing JSON.

protected boolean
supports(Class<?>
the class to test for support
clazz
)

Implements abstract org.springframework.messaging.converter.AbstractMessageConverter.supports.

Whether the given class is supported by this converter.

Inherited from org.springframework.messaging.converter.AbstractMessageConverter:
addSupportedMimeTypesfromMessagefromMessagegetContentTypeResolvergetDefaultContentTypegetMimeTypegetResolvedTypegetSerializedPayloadClassgetSupportedMimeTypesisStrictContentTypeMatchsetContentTypeResolversetSerializedPayloadClasssetStrictContentTypeMatchsupportsMimeTypetoMessagetoMessage

Field Detail

DEFAULT_MIME_TYPESback to summary
private static final MimeType[] DEFAULT_MIME_TYPES
objectMapperback to summary
private ObjectMapper objectMapper
prettyPrintback to summary
private Boolean prettyPrint
Annotations
@Nullable

Constructor Detail

MappingJackson2MessageConverterback to summary
public MappingJackson2MessageConverter()

Construct a MappingJackson2MessageConverter with a default ObjectMapper, supporting the application/json MIME type with UTF-8 character set.

MappingJackson2MessageConverterback to summary
public MappingJackson2MessageConverter(MimeType... supportedMimeTypes)

Construct a MappingJackson2MessageConverter with a default ObjectMapper, supporting one or more custom MIME types.

Parameters
supportedMimeTypes:MimeType[]

the supported MIME types

Annotations
@SuppressWarnings:deprecation
Since
4.1.5
MappingJackson2MessageConverterback to summary
public MappingJackson2MessageConverter(ObjectMapper objectMapper)

Construct a MappingJackson2MessageConverter with a custom ObjectMapper, supporting the application/json MIME type with UTF-8 character set.

Parameters
objectMapper:ObjectMapper

the ObjectMapper to use

Since
6.1
MappingJackson2MessageConverterback to summary
public MappingJackson2MessageConverter(ObjectMapper objectMapper, MimeType... supportedMimeTypes)

Construct a MappingJackson2MessageConverter with a custom ObjectMapper, supporting one or more custom MIME types.

Parameters
objectMapper:ObjectMapper

the ObjectMapper to use

supportedMimeTypes:MimeType[]

the supported MIME types

Since
6.1

Method Detail

canConvertFromback to summary
protected boolean canConvertFrom(Message<?> message, Class<?> targetClass)

Overrides org.springframework.messaging.converter.AbstractMessageConverter.canConvertFrom.

Annotations
@Override
canConvertToback to summary
protected boolean canConvertTo(Object payload, MessageHeaders headers)

Overrides org.springframework.messaging.converter.AbstractMessageConverter.canConvertTo.

Annotations
@Override
configurePrettyPrintback to summary
private void configurePrettyPrint()
convertFromInternalback to summary
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint)

Overrides org.springframework.messaging.converter.AbstractMessageConverter.convertFromInternal.

Doc from org.springframework.messaging.converter.AbstractMessageConverter.convertFromInternal.

Convert the message payload from serialized form to an Object.

Parameters
message:Message<?>

the input message

targetClass:Class<?>

the target class for the conversion

conversionHint:Object

an extra object passed to the MessageConverter, e.g. the associated MethodParameter (may be null}

Returns:Object

the result of the conversion, or null if the converter cannot perform the conversion

Annotations
@Override
@Nullable
convertToInternalback to summary
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint)

Overrides org.springframework.messaging.converter.AbstractMessageConverter.convertToInternal.

Doc from org.springframework.messaging.converter.AbstractMessageConverter.convertToInternal.

Convert the payload object to serialized form.

Parameters
payload:Object

the Object to convert

headers:MessageHeaders

optional headers for the message (may be null)

conversionHint:Object

an extra object passed to the MessageConverter, e.g. the associated MethodParameter (may be null}

Returns:Object

the resulting payload for the message, or null if the converter cannot perform the conversion

Annotations
@Override
@Nullable
extractViewClassback to summary
private Class<?> extractViewClass(JsonView annotation, Object conversionHint)
getJsonEncodingback to summary
protected JsonEncoding getJsonEncoding(MimeType contentType)

Determine the JSON encoding to use for the given content type.

Parameters
contentType:MimeType

the MIME type from the MessageHeaders, if any

Returns:JsonEncoding

the JSON encoding to use (never null)

getObjectMapperback to summary
public ObjectMapper getObjectMapper()

Return the underlying ObjectMapper for this converter.

getSerializationViewback to summary
protected Class<?> getSerializationView(Object conversionHint)

Determine a Jackson serialization view based on the given conversion hint.

Parameters
conversionHint:Object

the conversion hint Object as passed into the converter for the current conversion attempt

Returns:Class<?>

the serialization view class, or null if none

Annotations
@Nullable
Since
4.2
logWarningIfNecessaryback to summary
protected void logWarningIfNecessary(Type type, Throwable cause)

Determine whether to log the given exception coming from a ObjectMapper#canDeserialize / ObjectMapper#canSerialize check.

Parameters
type:Type

the class that Jackson tested for (de-)serializability

cause:Throwable

the Jackson-thrown exception to evaluate (typically a JsonMappingException)

Since
4.3
setObjectMapperback to summary
public void setObjectMapper(ObjectMapper objectMapper)

Set the 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 com.fasterxml.jackson.databind.ser.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.

setPrettyPrintback to summary
public void setPrettyPrint(boolean prettyPrint)

Whether to use the 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);
supportsback to summary
protected boolean supports(Class<?> clazz)

Implements abstract org.springframework.messaging.converter.AbstractMessageConverter.supports.

Doc from org.springframework.messaging.converter.AbstractMessageConverter.supports.

Whether the given class is supported by this converter.

Parameters
clazz:Class<?>

the class to test for support

Returns:boolean

true if supported; false otherwise

Annotations
@Override