Class AbstractMessageConverter
- All Implemented Interfaces:
MessageConverter
,SmartMessageConverter
- Direct Known Subclasses:
AbstractJsonMessageConverter
,ByteArrayMessageConverter
,MappingJackson2MessageConverter
,MarshallingMessageConverter
,ProtobufMessageConverter
,StringMessageConverter
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.- Since:
- 4.0
- Author:
- Rossen Stoyanchev, Sebastien Deleuze, Juergen Hoeller
-
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractMessageConverter
(Collection<MimeType> supportedMimeTypes) Constructor with a Collection of MIME types.protected
AbstractMessageConverter
(MimeType supportedMimeType) Constructor with a single MIME type.protected
AbstractMessageConverter
(MimeType... supportedMimeTypes) Constructor with one or more MIME types via vararg. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addSupportedMimeTypes
(MimeType... supportedMimeTypes) Allows subclasses to add more supported mime types.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.final Object
fromMessage
(Message<?> message, Class<?> targetClass) Convert the payload of aMessage
from a serialized form to a typed Object of the specified target class.final Object
fromMessage
(Message<?> message, Class<?> targetClass, Object conversionHint) A variant ofMessageConverter.fromMessage(Message, Class)
which takes an extra conversion context as an argument, allowing to take e.g.Return the configuredContentTypeResolver
.protected MimeType
getDefaultContentType
(Object payload) Return the default content type for the payload.protected MimeType
getMimeType
(MessageHeaders headers) Class<?>
Return the configured preferred serialization payload class.Return the supported MIME types.boolean
Whether content type resolution must produce a value that matches one of the supported MIME types.void
setContentTypeResolver
(ContentTypeResolver resolver) Configure theContentTypeResolver
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 aMessage
.void
setStrictContentTypeMatch
(boolean strictContentTypeMatch) Whether this converter should convert messages for which no content type could be resolved through the configuredContentTypeResolver
.protected abstract boolean
Whether the given class is supported by this converter.protected boolean
supportsMimeType
(MessageHeaders headers) final Message<?>
toMessage
(Object payload, MessageHeaders headers) Create aMessage
whose payload is the result of converting the given payload Object to serialized form.final Message<?>
toMessage
(Object payload, MessageHeaders headers, Object conversionHint) A variant ofMessageConverter.toMessage(Object, MessageHeaders)
which takes an extra conversion context as an argument, allowing to take e.g.
-
Field Details
-
logger
-
-
Constructor Details
-
AbstractMessageConverter
Constructor with a single MIME type.- Parameters:
supportedMimeType
- the supported MIME type
-
AbstractMessageConverter
Constructor with one or more MIME types via vararg.- Parameters:
supportedMimeTypes
- the supported MIME types- Since:
- 5.2.2
-
AbstractMessageConverter
Constructor with a Collection of MIME types.- Parameters:
supportedMimeTypes
- the supported MIME types
-
-
Method Details
-
getSupportedMimeTypes
Return the supported MIME types. -
addSupportedMimeTypes
Allows subclasses to add more supported mime types.- Since:
- 5.2.2
-
setContentTypeResolver
Configure theContentTypeResolver
to use to resolve the content type of an input message.Note that if no resolver is configured, then
strictContentTypeMatch
should be left asfalse
(the default) or otherwise this converter will ignore all messages.By default, a
DefaultContentTypeResolver
instance is used. -
getContentTypeResolver
Return the configuredContentTypeResolver
. -
setStrictContentTypeMatch
public void setStrictContentTypeMatch(boolean strictContentTypeMatch) Whether this converter should convert messages for which no content type could be resolved through the configuredContentTypeResolver
.A converter can be configured to be strict only when a
contentTypeResolver
is configured and the list ofsupportedMimeTypes
is not be empty.When this flag is set to
true
,supportsMimeType(MessageHeaders)
will returnfalse
if thecontentTypeResolver
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
Configure the preferred serialization class to use (byte[] or String) when converting an Object payload to aMessage
.The default value is byte[].
- Parameters:
payloadClass
- either byte[] or String
-
getSerializedPayloadClass
Return the configured preferred serialization payload class. -
fromMessage
Description copied from interface:MessageConverter
Convert the payload of aMessage
from a serialized form to a typed Object of the specified target class. TheMessageHeaders.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 interfaceMessageConverter
- Parameters:
message
- the input messagetargetClass
- the target class for the conversion- Returns:
- the result of the conversion, or
null
if the converter cannot perform the conversion
-
fromMessage
@Nullable public final Object fromMessage(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) Description copied from interface:SmartMessageConverter
A variant ofMessageConverter.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 interfaceSmartMessageConverter
- Parameters:
message
- the input messagetargetClass
- the target class for the conversionconversionHint
- an extra object passed to theMessageConverter
, e.g. the associatedMethodParameter
(may benull
}- Returns:
- the result of the conversion, or
null
if the converter cannot perform the conversion - See Also:
-
toMessage
Description copied from interface:MessageConverter
Create aMessage
whose payload is the result of converting the given payload Object to serialized form. The optionalMessageHeaders
parameter may contain aMessageHeaders.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 interfaceMessageConverter
- Parameters:
payload
- the Object to convertheaders
- optional headers for the message (may benull
)- Returns:
- the new message, or
null
if the converter does not support the Object type or the target media type
-
toMessage
@Nullable public final Message<?> toMessage(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) Description copied from interface:SmartMessageConverter
A variant ofMessageConverter.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 interfaceSmartMessageConverter
- Parameters:
payload
- the Object to convertheaders
- optional headers for the message (may benull
)conversionHint
- an extra object passed to theMessageConverter
, e.g. the associatedMethodParameter
(may benull
}- Returns:
- the new message, or
null
if the converter does not support the Object type or the target media type - See Also:
-
canConvertFrom
-
canConvertTo
-
supportsMimeType
-
getMimeType
-
getDefaultContentType
Return the default content type for the payload. Called whentoMessage(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
- the payload being converted to a message- Returns:
- the content type, or
null
if not known
-
supports
Whether the given class is supported by this converter.- 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) Convert the message payload from serialized form to an Object.- Parameters:
message
- the input messagetargetClass
- the target class for the conversionconversionHint
- an extra object passed to theMessageConverter
, e.g. the associatedMethodParameter
(may benull
}- Returns:
- the result of the conversion, or
null
if the converter cannot perform the conversion - Since:
- 4.2
-
convertToInternal
@Nullable protected Object convertToInternal(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) Convert the payload object to serialized form.- Parameters:
payload
- the Object to convertheaders
- optional headers for the message (may benull
)conversionHint
- an extra object passed to theMessageConverter
, e.g. the associatedMethodParameter
(may benull
}- Returns:
- the resulting payload for the message, or
null
if the converter cannot perform the conversion - Since:
- 4.2
-