org.springframework.messaging.converter

## Class MappingJackson2MessageConverter

- java.lang.Object
- - org.springframework.messaging.converter.AbstractMessageConverter
  - - org.springframework.messaging.converter.MappingJackson2MessageConverter

All Implemented Interfaces:
  MessageConverter, SmartMessageConverter

    public class MappingJackson2MessageConverter
    extends AbstractMessageConverter

A Jackson 2 based `MessageConverter` implementation.

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

- `MapperFeature.DEFAULT_VIEW_INCLUSION` is disabled
- `DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES` is disabled

Compatible with Jackson 2.9 and higher, as of Spring 5.1.

Since:
  4.0
Author:
  Rossen Stoyanchev, Juergen Hoeller, Sebastien Deleuze

### Field Summary

### Fields inherited from class org.springframework.messaging.converter.AbstractMessageConverter

`logger`

### Constructor Summary

Constructors
Constructor and Description
`MappingJackson2MessageConverter()` Construct a `MappingJackson2MessageConverter` supporting the `application/json` MIME type with `UTF-8` character set.
`MappingJackson2MessageConverter(org.springframework.util.MimeType... supportedMimeTypes)` Construct a `MappingJackson2MessageConverter` supporting one or more custom MIME types.

### Method Summary

All Methods Instance Methods Concrete Methods
`protected 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(org.springframework.util.MimeType contentType)` Determine the JSON encoding to use for the given content type.
`ObjectMapper` `getObjectMapper()` Return the underlying `ObjectMapper` 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 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(Class<?> clazz)` 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`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### MappingJackson2MessageConverter

    public MappingJackson2MessageConverter()

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

#### MappingJackson2MessageConverter

    public MappingJackson2MessageConverter(org.springframework.util.MimeType... supportedMimeTypes)

Construct a `MappingJackson2MessageConverter` supporting one or more custom MIME types.

Parameters:
  `supportedMimeTypes` - the supported MIME types
Since:
  4.1.5

### Method Detail

#### setObjectMapper

    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 `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.

#### getObjectMapper

    public ObjectMapper getObjectMapper()

Return the underlying `ObjectMapper` for this converter.

#### setPrettyPrint

    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);

#### canConvertFrom

    protected boolean canConvertFrom(Message<?> message,
                                     @Nullable
                                     Class<?> targetClass)

Overrides:
  `canConvertFrom` in class `AbstractMessageConverter`

#### canConvertTo

    protected boolean canConvertTo(Object payload,
                                   @Nullable
                                   MessageHeaders headers)

Overrides:
  `canConvertTo` in class `AbstractMessageConverter`

#### logWarningIfNecessary

    protected void logWarningIfNecessary(Type type,
                                         @Nullable
                                         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.

Parameters:
  `type` - the class that Jackson tested for (de-)serializability
  `cause` - the Jackson-thrown exception to evaluate (typically a `JsonMappingException`)
Since:
  4.3

#### supports

    protected boolean supports(Class<?> clazz)

Description copied from class: `AbstractMessageConverter`
Whether the given class is supported by this converter.

Specified by:
  `supports` in class `AbstractMessageConverter`
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 class `AbstractMessageConverter`
Parameters:
  `message` - the input message
  `targetClass` - the target class for the conversion
  `conversionHint` - an extra object passed to the `MessageConverter`, e.g. the associated `MethodParameter` (may be `null`}
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 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`, e.g. the associated `MethodParameter` (may be `null`}
Returns:
  the resulting payload for the message, or `null` if the converter cannot perform the conversion

#### getSerializationView

    @Nullable
    protected Class<?> getSerializationView(@Nullable
                                                      Object conversionHint)

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

    protected JsonEncoding getJsonEncoding(@Nullable
                                           org.springframework.util.MimeType contentType)

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`)