org.springframework.messaging.converter

## Class MappingJackson2MessageConverter

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

- All Implemented Interfaces:
    MessageConverter

      public class MappingJackson2MessageConverter
      extends AbstractMessageConverter

  A Jackson 2 based `MessageConverter` implementation.

  Tested against Jackson 2.2 and 2.3; compatible with Jackson 2.0 and higher.

  Since:
    4.0
  Author:
    Rossen Stoyanchev, Juergen Hoeller

- - ### Field Summary

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

      `logger`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `MappingJackson2MessageConverter()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected boolean` | `canConvertFrom(Message<?> message, Class<?> targetClass)`
    `protected boolean` | `canConvertTo(Object payload, MessageHeaders headers)`
    `Object` | `convertFromInternal(Message<?> message, Class<?> targetClass)` Convert the message payload from serialized form to an Object.
    `Object` | `convertToInternal(Object payload, MessageHeaders headers)` 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.
    `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

      `fromMessage, getContentTypeResolver, getDefaultContentType, getMimeType, getSerializedPayloadClass, getSupportedMimeTypes, isStrictContentTypeMatch, setContentTypeResolver, setSerializedPayloadClass, setStrictContentTypeMatch, supportsMimeType, toMessage`

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

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

- - ### Constructor Detail

    - #### MappingJackson2MessageConverter

          public MappingJackson2MessageConverter()

  - ### 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,
                                           Class<?> targetClass)

      Overrides:
        `canConvertFrom` in class `AbstractMessageConverter`

    - #### canConvertTo

          protected boolean canConvertTo(Object payload,
                                         MessageHeaders headers)

      Overrides:
        `canConvertTo` in class `AbstractMessageConverter`

    - #### 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

          public Object convertFromInternal(Message<?> message,
                                            Class<?> targetClass)

      Description copied from class: `AbstractMessageConverter`
      Convert the message payload from serialized form to an Object.
      Specified by:
        `convertFromInternal` in class `AbstractMessageConverter`

    - #### convertToInternal

          public Object convertToInternal(Object payload,
                                          MessageHeaders headers)

      Description copied from class: `AbstractMessageConverter`
      Convert the payload object to serialized form.
      Specified by:
        `convertToInternal` in class `AbstractMessageConverter`

    - #### getJsonEncoding

          protected JsonEncoding getJsonEncoding(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`)