org.springframework.messaging.converter

## Class CompositeMessageConverter

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

All Implemented Interfaces:
  MessageConverter, SmartMessageConverter

    public class CompositeMessageConverter
    extends Object
    implements SmartMessageConverter

A `MessageConverter` that delegates to a list of registered converters to be invoked until one of them returns a non-null result.

As of 4.2.1, this composite converter implements `SmartMessageConverter` in order to support the delegation of conversion hints.

Since:
  4.0
Author:
  Rossen Stoyanchev, Juergen Hoeller

### Constructor Summary

Constructors
Constructor and Description
`CompositeMessageConverter(Collection<MessageConverter> converters)` Create an instance with the given converters.

### Method Summary

All Methods Instance Methods Concrete Methods
`Object` `fromMessage(Message<?> message, Class<?> targetClass)` Convert the payload of a `Message` from a serialized form to a typed Object of the specified target class.
`Object` `fromMessage(Message<?> message, Class<?> targetClass, Object conversionHint)` A variant of `MessageConverter.fromMessage(Message, Class)` which takes an extra conversion context as an argument, allowing to take e.g.
`List<MessageConverter>` `getConverters()` Return the underlying list of delegate converters.
`Message<?>` `toMessage(Object payload, MessageHeaders headers)` Create a `Message` whose payload is the result of converting the given payload Object to serialized form.
`Message<?>` `toMessage(Object payload, MessageHeaders headers, Object conversionHint)` A variant of `MessageConverter.toMessage(Object, MessageHeaders)` which takes an extra conversion context as an argument, allowing to take e.g.
`String` `toString()`

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

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

### Constructor Detail

#### CompositeMessageConverter

    public CompositeMessageConverter(Collection<MessageConverter> converters)

Create an instance with the given converters.

### Method Detail

#### fromMessage

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

Description copied from interface: `MessageConverter`
Convert the payload of a `Message` from a 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

#### fromMessage

    @Nullable
    public Object fromMessage(Message<?> message,
                                        Class<?> targetClass,
                                        @Nullable
                                        Object conversionHint)

Description copied from interface: `SmartMessageConverter`
A variant of `MessageConverter.fromMessage(Message, Class)` which takes an extra conversion context as an argument, allowing to take e.g. annotations on a payload parameter into account.

Specified by:
  `fromMessage` in interface `SmartMessageConverter`
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
See Also:
  `MessageConverter.fromMessage(Message, Class)`

#### toMessage

    @Nullable
    public Message<?> toMessage(Object payload,
                                          @Nullable
                                          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

#### toMessage

    @Nullable
    public Message<?> toMessage(Object payload,
                                          @Nullable
                                          MessageHeaders headers,
                                          @Nullable
                                          Object conversionHint)

Description copied from interface: `SmartMessageConverter`
A variant of `MessageConverter.toMessage(Object, MessageHeaders)` which takes an extra conversion context as an argument, allowing to take e.g. annotations on a return type into account.

Specified by:
  `toMessage` in interface `SmartMessageConverter`
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 new message, or `null` if the converter does not support the Object type or the target media type
See Also:
  `MessageConverter.toMessage(Object, MessageHeaders)`

#### getConverters

    public List<MessageConverter> getConverters()

Return the underlying list of delegate converters.

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`