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

public abstract Class AbstractMessageConverter

extends Object
implements SmartMessageConverter
Class Inheritance
All Implemented Interfaces
org.springframework.messaging.converter.SmartMessageConverter, org.springframework.messaging.converter.MessageConverter
Known Direct Subclasses
org.springframework.messaging.converter.ByteArrayMessageConverter, org.springframework.messaging.converter.MappingJackson2MessageConverter, org.springframework.messaging.converter.MarshallingMessageConverter, org.springframework.messaging.converter.ProtobufMessageConverter, org.springframework.messaging.converter.StringMessageConverter, org.springframework.messaging.converter.AbstractJsonMessageConverter
Imports
java.lang.reflect.Type, java.util.ArrayList, .Arrays, .Collection, .Collections, .List, org.apache.commons.logging.Log, .LogFactory, org.springframework.core.GenericTypeResolver, .MethodParameter, org.springframework.lang.Nullable, org.springframework.messaging.Message, .MessageHeaders, org.springframework.messaging.support.MessageBuilder, .MessageHeaderAccessor, org.springframework.util.Assert, .MimeType

Abstract base class for SmartMessageConverter 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.
Authors
Rossen Stoyanchev, Sebastien Deleuze, Juergen Hoeller
Since
4.0

Field Summary

Modifier and TypeField and Description
private ContentTypeResolver
protected final Log
private Class<?>
private boolean
private final List<MimeType>

Constructor Summary

AccessConstructor and Description
protected
AbstractMessageConverter(MimeType supportedMimeType)

Constructor with a single MIME type.

protected
AbstractMessageConverter(MimeType... supportedMimeTypes)

Constructor with multiple MIME types.

protected
AbstractMessageConverter(Collection<MimeType> supportedMimeTypes)

Constructor with Collection of MIME types.

Method Summary

Modifier and TypeMethod and Description
protected void
addSupportedMimeTypes(MimeType... supportedMimeTypes)

Allows subclasses to add more supported mime types.

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

protected boolean
protected Object

Returns:

the result of the conversion, or null if the converter cannot perform the conversion
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
)

Convert the message payload from serialized form to an Object.

protected Object

Returns:

the resulting payload for the message, or null if the converter cannot perform the conversion
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
)

Convert the payload object to serialized form.

public final Object
fromMessage(Message<?>
the input message
message
,
Class<?>
the target class for the conversion
targetClass
)

Implements org.springframework.messaging.converter.MessageConverter.fromMessage.

Convert the payload of a Message from a serialized form to a typed Object of the specified target class.

public final Object
fromMessage(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
)

Implements org.springframework.messaging.converter.SmartMessageConverter.fromMessage.

A variant of fromMessage(Message, Class) which takes an extra conversion context as an argument, allowing to take e.g. annotations on a payload parameter into account.

public ContentTypeResolver
getContentTypeResolver()

Return the configured ContentTypeResolver.

protected MimeType

Returns:

the content type, or null if not known
getDefaultContentType
(Object
the payload being converted to a message
payload
)

Return the default content type for the payload.

protected MimeType
pack-priv static Type
getResolvedType(Class<?> targetClass, Object conversionHint)

public Class<?>
getSerializedPayloadClass()

Return the configured preferred serialization payload class.

public List<MimeType>
getSupportedMimeTypes()

Return the supported MIME types.

public boolean
isStrictContentTypeMatch()

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

public void
setContentTypeResolver(ContentTypeResolver resolver)

Configure a ContentTypeResolver for resolving the content type of input messages.

public void
setSerializedPayloadClass(Class<?>
either byte[] or String
payloadClass
)

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

public void
setStrictContentTypeMatch(boolean strictContentTypeMatch)

Whether this converter should convert messages for which no content type can be resolved through the configured org.springframework.messaging.converter.ContentTypeResolver.

protected abstract boolean

Returns:

true if supported; false otherwise
supports
(Class<?>
the class to test for support
clazz
)

Whether the given class is supported by this converter.

protected boolean
public final Message<?>
toMessage(Object
the Object to convert
payload
,
MessageHeaders
optional headers for the message (may be null)
headers
)

Implements org.springframework.messaging.converter.MessageConverter.toMessage.

Create a Message whose payload is the result of converting the given payload Object to serialized form.

public final Message<?>
toMessage(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
)

Implements org.springframework.messaging.converter.SmartMessageConverter.toMessage.

A variant of toMessage(Object, MessageHeaders) which takes an extra conversion context as an argument, allowing to take e.g. annotations on a return type into account.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

contentTypeResolverback to summary
private ContentTypeResolver contentTypeResolver
Annotations
@Nullable
loggerback to summary
protected final Log logger
serializedPayloadClassback to summary
private Class<?> serializedPayloadClass
strictContentTypeMatchback to summary
private boolean strictContentTypeMatch
supportedMimeTypesback to summary
private final List<MimeType> supportedMimeTypes

Constructor Detail

AbstractMessageConverterback to summary
protected AbstractMessageConverter(MimeType supportedMimeType)

Constructor with a single MIME type.

AbstractMessageConverterback to summary
protected AbstractMessageConverter(MimeType... supportedMimeTypes)

Constructor with multiple MIME types.

Since
5.2.2
AbstractMessageConverterback to summary
protected AbstractMessageConverter(Collection<MimeType> supportedMimeTypes)

Constructor with Collection of MIME types.

Method Detail

addSupportedMimeTypesback to summary
protected void addSupportedMimeTypes(MimeType... supportedMimeTypes)

Allows subclasses to add more supported mime types.

Since
5.2.2
canConvertFromback to summary
protected boolean canConvertFrom(Message<?> message, Class<?> targetClass)
canConvertToback to summary
protected boolean canConvertTo(Object payload, MessageHeaders headers)
convertFromInternalback to summary
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint)

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
@Nullable
Since
4.2
convertToInternalback to summary
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint)

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
@Nullable
Since
4.2
fromMessageback to summary
public final Object fromMessage(Message<?> message, Class<?> targetClass)

Implements org.springframework.messaging.converter.MessageConverter.fromMessage.

Doc from org.springframework.messaging.converter.MessageConverter.fromMessage.

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.

Parameters
message:Message<?>

the input message

targetClass:Class<?>

the target class for the conversion

Returns:Object

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

Annotations
@Override
@Nullable
fromMessageback to summary
public final Object fromMessage(Message<?> message, Class<?> targetClass, Object conversionHint)

Implements org.springframework.messaging.converter.SmartMessageConverter.fromMessage.

Doc from org.springframework.messaging.converter.SmartMessageConverter.fromMessage.

A variant of fromMessage(Message, Class) which takes an extra conversion context as an argument, allowing to take e.g. annotations on a payload parameter into account.

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
getContentTypeResolverback to summary
public ContentTypeResolver getContentTypeResolver()

Return the configured ContentTypeResolver.

Annotations
@Nullable
getDefaultContentTypeback to summary
protected MimeType getDefaultContentType(Object payload)

Return 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 subclasses.

Parameters
payload:Object

the payload being converted to a message

Returns:MimeType

the content type, or null if not known

Annotations
@Nullable
getMimeTypeback to summary
protected MimeType getMimeType(MessageHeaders headers)
Annotations
@Nullable
getResolvedTypeback to summary
pack-priv static Type getResolvedType(Class<?> targetClass, Object conversionHint)
getSerializedPayloadClassback to summary
public Class<?> getSerializedPayloadClass()

Return the configured preferred serialization payload class.

getSupportedMimeTypesback to summary
public List<MimeType> getSupportedMimeTypes()

Return the supported MIME types.

isStrictContentTypeMatchback to summary
public boolean isStrictContentTypeMatch()

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

setContentTypeResolverback to summary
public void setContentTypeResolver(ContentTypeResolver resolver)

Configure a ContentTypeResolver for resolving the content type of input messages.

By default, a DefaultContentTypeResolver instance is used.

Note

if the resolver is set to null, then strictContentTypeMatch should be false, which is the default, or otherwise this converter will ignore all messages.

setSerializedPayloadClassback to summary
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:Class<?>

either byte[] or String

setStrictContentTypeMatchback to summary
public void setStrictContentTypeMatch(boolean strictContentTypeMatch)

Whether this converter should convert messages for which no content type can be resolved through the configured org.springframework.messaging.converter.ContentTypeResolver.

A converter can be configured to be strict only when a contentTypeResolver is configured and the list of supportedMimeTypes is not empty.

When this flag is set to true, supportsMimeType(MessageHeaders) will return false if the contentTypeResolver is not defined or if no content-type header is present.

supportsback to summary
protected abstract boolean supports(Class<?> clazz)

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

supportsMimeTypeback to summary
protected boolean supportsMimeType(MessageHeaders headers)
toMessageback to summary
public final Message<?> toMessage(Object payload, MessageHeaders headers)

Implements org.springframework.messaging.converter.MessageConverter.toMessage.

Doc from org.springframework.messaging.converter.MessageConverter.toMessage.

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.

Parameters
payload:Object

the Object to convert

headers:MessageHeaders

optional headers for the message (may be null)

Returns:Message<?>

the new message, or null if the converter does not support the Object type or the target media type

Annotations
@Override
@Nullable
toMessageback to summary
public final Message<?> toMessage(Object payload, MessageHeaders headers, Object conversionHint)

Implements org.springframework.messaging.converter.SmartMessageConverter.toMessage.

Doc from org.springframework.messaging.converter.SmartMessageConverter.toMessage.

A variant of toMessage(Object, MessageHeaders) which takes an extra conversion context as an argument, allowing to take e.g. annotations on a return type into account.

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:Message<?>

the new message, or null if the converter does not support the Object type or the target media type

Annotations
@Override
@Nullable