org.springframework.messaging.converter

## Class AbstractMessageConverter

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

- All Implemented Interfaces:
    MessageConverter
  Direct Known Subclasses:
    ByteArrayMessageConverter, MappingJackson2MessageConverter, StringMessageConverter

      public abstract class AbstractMessageConverter
      extends Object
      implements MessageConverter

  Abstract base class for `MessageConverter` implementations including support for common properties and a partial implementation of the conversion methods mainly to check if the converter supports the conversion based on the payload class and MIME type.
  Since:
    4.0
  Author:
    Rossen Stoyanchev

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected Log` | `logger`

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `AbstractMessageConverter(Collection<MimeType> supportedMimeTypes)` Construct an `AbstractMessageConverter` supporting multiple MIME types.
    `protected` | `AbstractMessageConverter(MimeType supportedMimeType)` Construct an `AbstractMessageConverter` supporting a single MIME type.

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected boolean` | `canConvertFrom(Message<?> message, Class<?> targetClass)`
    `protected boolean` | `canConvertTo(Object payload, MessageHeaders headers)`
    `abstract Object` | `convertFromInternal(Message<?> message, Class<?> targetClass)` Convert the message payload from serialized form to an Object.
    `abstract Object` | `convertToInternal(Object payload, MessageHeaders headers)` Convert the payload object to serialized form.
    `Object` | `fromMessage(Message<?> message, Class<?> targetClass)` Convert the payload of a `Message` from serialized form to a typed Object of the specified target class.
    `ContentTypeResolver` | `getContentTypeResolver()` Return the configured `ContentTypeResolver`.
    `protected MimeType` | `getDefaultContentType(Object payload)` Returns the default content type for the payload.
    `protected MimeType` | `getMimeType(MessageHeaders headers)`
    `Class<?>` | `getSerializedPayloadClass()` Return the configured preferred serialization payload class.
    `List<MimeType>` | `getSupportedMimeTypes()` Return the supported MIME types.
    `boolean` | `isStrictContentTypeMatch()` Whether content type resolution must produce a value that matches one of the supported MIME types.
    `void` | `setContentTypeResolver(ContentTypeResolver resolver)` Configure the `ContentTypeResolver` to use to resolve the content type of an input message.
    `void` | `setSerializedPayloadClass(Class<?> payloadClass)` Configure the preferred serialization class to use (byte[] or String) when converting an Object payload to a `Message`.
    `void` | `setStrictContentTypeMatch(boolean strictContentTypeMatch)` Whether this converter should convert messages for which no content type could be resolved through the configured `ContentTypeResolver`.
    `protected abstract boolean` | `supports(Class<?> clazz)` Whether the given class is supported by this converter.
    `protected boolean` | `supportsMimeType(MessageHeaders headers)`
    `Message<?>` | `toMessage(Object payload, MessageHeaders headers)` Create a `Message` whose payload is the result of converting the given payload Object to serialized form.

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

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

- - ### Field Detail

    - #### logger

          protected final Log logger

  - ### Constructor Detail

    - #### AbstractMessageConverter

          protected AbstractMessageConverter(MimeType supportedMimeType)

      Construct an `AbstractMessageConverter` supporting a single MIME type.
      Parameters:
        `supportedMimeType` - the supported MIME type

    - #### AbstractMessageConverter

          protected AbstractMessageConverter(Collection<MimeType> supportedMimeTypes)

      Construct an `AbstractMessageConverter` supporting multiple MIME types.
      Parameters:
        `supportedMimeTypes` - the supported MIME types

  - ### Method Detail

    - #### getSupportedMimeTypes

          public List<MimeType> getSupportedMimeTypes()

      Return the supported MIME types.

    - #### setContentTypeResolver

          public void setContentTypeResolver(ContentTypeResolver resolver)

      Configure the `ContentTypeResolver` to use to resolve the content type of an input message.

      Note that if no resolver is configured, then `strictContentTypeMatch` should be left as `false` (the default) or otherwise this converter will ignore all messages.

      By default, a `DefaultContentTypeResolver` instance is used.

    - #### getContentTypeResolver

          public ContentTypeResolver getContentTypeResolver()

      Return the configured `ContentTypeResolver`.

    - #### setStrictContentTypeMatch

          public void setStrictContentTypeMatch(boolean strictContentTypeMatch)

      Whether this converter should convert messages for which no content type could be resolved through the configured `ContentTypeResolver`. A converter can configured to be strict only when a `contentTypeResolver` is configured and the list of `supportedMimeTypes` is not be empty. then requires the content type of a message to be resolved When set to true, #supportsMimeType(MessageHeaders) will return false if the contentTypeResolver is not defined or if no content-type header is present.

    - #### isStrictContentTypeMatch

          public boolean isStrictContentTypeMatch()

      Whether content type resolution must produce a value that matches one of the supported MIME types.

    - #### setSerializedPayloadClass

          public void setSerializedPayloadClass(Class<?> payloadClass)

      Configure the preferred serialization class to use (byte[] or String) when converting an Object payload to a `Message`.

      The default value is byte[].

      Parameters:
        `payloadClass` - either byte[] or String

    - #### getSerializedPayloadClass

          public Class<?> getSerializedPayloadClass()

      Return the configured preferred serialization payload class.

    - #### getDefaultContentType

          protected MimeType getDefaultContentType(Object payload)

      Returns the default content type for the payload. Called when `toMessage(Object, MessageHeaders)` is invoked without message headers or without a content type header.

      By default, this returns the first element of the `supportedMimeTypes`, if any. Can be overridden in sub-classes.

      Parameters:
        `payload` - the payload being converted to message
      Returns:
        the content type, or `null` if not known

    - #### supports

          protected abstract boolean supports(Class<?> clazz)

      Whether the given class is supported by this converter.
      Parameters:
        `clazz` - the class to test for support
      Returns:
        `true` if supported; `false` otherwise

    - #### fromMessage

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

      Description copied from interface: `MessageConverter`
      Convert the payload of a `Message` from serialized form to a typed Object of the specified target class. The `MessageHeaders.CONTENT_TYPE` header should indicate the MIME type to convert from.

      If the converter does not support the specified media type or cannot perform the conversion, it should return `null`.

      Specified by:
        `fromMessage` in interface `MessageConverter`
      Parameters:
        `message` - the input message
        `targetClass` - the target class for the conversion
      Returns:
        the result of the conversion or `null` if the converter cannot perform the conversion

    - #### canConvertFrom

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

    - #### convertFromInternal

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

      Convert the message payload from serialized form to an Object.

    - #### toMessage

          public final Message<?> toMessage(Object payload,
                                            MessageHeaders headers)

      Description copied from interface: `MessageConverter`
      Create a `Message` whose payload is the result of converting the given payload Object to serialized form. The optional `MessageHeaders` parameter may contain a `MessageHeaders.CONTENT_TYPE` header to specify the target media type for the conversion and it may contain additional headers to be added to the message.

      If the converter does not support the specified media type or cannot perform the conversion, it should return `null`.

      Specified by:
        `toMessage` in interface `MessageConverter`
      Parameters:
        `payload` - the Object to convert
        `headers` - optional headers for the message, may be `null`
      Returns:
        the new message or `null` if the converter does not support the Object type or the target media type

    - #### canConvertTo

          protected boolean canConvertTo(Object payload,
                                         MessageHeaders headers)

    - #### convertToInternal

          public abstract Object convertToInternal(Object payload,
                                                   MessageHeaders headers)

      Convert the payload object to serialized form.

    - #### supportsMimeType

          protected boolean supportsMimeType(MessageHeaders headers)

    - #### getMimeType

          protected MimeType getMimeType(MessageHeaders headers)