public abstract class AbstractMessageConverter extends java.lang.Object implements SmartMessageConverter
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.Modifier | Constructor and Description |
---|---|
protected |
AbstractMessageConverter(java.util.Collection<MimeType> supportedMimeTypes)
Construct an
AbstractMessageConverter supporting multiple MIME types. |
protected |
AbstractMessageConverter(MimeType supportedMimeType)
Construct an
AbstractMessageConverter supporting a single MIME type. |
Modifier and Type | Method and Description |
---|---|
protected boolean |
canConvertFrom(Message<?> message,
java.lang.Class<?> targetClass) |
protected boolean |
canConvertTo(java.lang.Object payload,
MessageHeaders headers) |
protected java.lang.Object |
convertFromInternal(Message<?> message,
java.lang.Class<?> targetClass,
java.lang.Object conversionHint)
Convert the message payload from serialized form to an Object.
|
protected java.lang.Object |
convertToInternal(java.lang.Object payload,
MessageHeaders headers,
java.lang.Object conversionHint)
Convert the payload object to serialized form.
|
java.lang.Object |
fromMessage(Message<?> message,
java.lang.Class<?> targetClass)
Convert the payload of a
Message from a serialized form to a typed Object
of the specified target class. |
java.lang.Object |
fromMessage(Message<?> message,
java.lang.Class<?> targetClass,
java.lang.Object conversionHint)
A variant of
MessageConverter.fromMessage(Message, Class) which takes an extra
conversion context as an argument, allowing to take e.g. |
ContentTypeResolver |
getContentTypeResolver()
Return the configured
ContentTypeResolver . |
protected MimeType |
getDefaultContentType(java.lang.Object payload)
Returns the default content type for the payload.
|
protected MimeType |
getMimeType(MessageHeaders headers) |
java.lang.Class<?> |
getSerializedPayloadClass()
Return the configured preferred serialization payload class.
|
java.util.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(java.lang.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(java.lang.Class<?> clazz)
Whether the given class is supported by this converter.
|
protected boolean |
supportsMimeType(MessageHeaders headers) |
Message<?> |
toMessage(java.lang.Object payload,
MessageHeaders headers)
Create a
Message whose payload is the result of converting the given
payload Object to serialized form. |
Message<?> |
toMessage(java.lang.Object payload,
MessageHeaders headers,
java.lang.Object conversionHint)
A variant of
MessageConverter.toMessage(Object, MessageHeaders) which takes an extra
conversion context as an argument, allowing to take e.g. |
protected final Log logger
protected AbstractMessageConverter(MimeType supportedMimeType)
AbstractMessageConverter
supporting a single MIME type.supportedMimeType
- the supported MIME typeprotected AbstractMessageConverter(java.util.Collection<MimeType> supportedMimeTypes)
AbstractMessageConverter
supporting multiple MIME types.supportedMimeTypes
- the supported MIME typespublic java.util.List<MimeType> getSupportedMimeTypes()
public void setContentTypeResolver(@Nullable ContentTypeResolver resolver)
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.
@Nullable public ContentTypeResolver getContentTypeResolver()
ContentTypeResolver
.public void setStrictContentTypeMatch(boolean strictContentTypeMatch)
ContentTypeResolver
.
A converter can configured to be strict only when a
contentTypeResolver
is configured and the
list of supportedMimeTypes
is not be 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.
public boolean isStrictContentTypeMatch()
public void setSerializedPayloadClass(java.lang.Class<?> payloadClass)
Message
.
The default value is byte[].
payloadClass
- either byte[] or Stringpublic java.lang.Class<?> getSerializedPayloadClass()
@Nullable protected MimeType getDefaultContentType(java.lang.Object payload)
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.
payload
- the payload being converted to messagenull
if not known@Nullable public final java.lang.Object fromMessage(Message<?> message, java.lang.Class<?> targetClass)
MessageConverter
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
.
fromMessage
in interface MessageConverter
message
- the input messagetargetClass
- the target class for the conversionnull
if the converter cannot
perform the conversion@Nullable public final java.lang.Object fromMessage(Message<?> message, java.lang.Class<?> targetClass, @Nullable java.lang.Object conversionHint)
SmartMessageConverter
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.fromMessage
in interface SmartMessageConverter
message
- the input messagetargetClass
- the target class for the conversionconversionHint
- an extra object passed to the MessageConverter
,
e.g. the associated MethodParameter
(may be null
}null
if the converter cannot
perform the conversionMessageConverter.fromMessage(Message, Class)
protected boolean canConvertFrom(Message<?> message, java.lang.Class<?> targetClass)
@Nullable public final Message<?> toMessage(java.lang.Object payload, @Nullable MessageHeaders headers)
MessageConverter
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
.
toMessage
in interface MessageConverter
payload
- the Object to convertheaders
- optional headers for the message (may be null
)null
if the converter does not support the
Object type or the target media type@Nullable public final Message<?> toMessage(java.lang.Object payload, @Nullable MessageHeaders headers, @Nullable java.lang.Object conversionHint)
SmartMessageConverter
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.toMessage
in interface SmartMessageConverter
payload
- the Object to convertheaders
- optional headers for the message (may be null
)conversionHint
- an extra object passed to the MessageConverter
,
e.g. the associated MethodParameter
(may be null
}null
if the converter does not support the
Object type or the target media typeMessageConverter.toMessage(Object, MessageHeaders)
protected boolean canConvertTo(java.lang.Object payload, @Nullable MessageHeaders headers)
protected boolean supportsMimeType(@Nullable MessageHeaders headers)
@Nullable protected MimeType getMimeType(@Nullable MessageHeaders headers)
protected abstract boolean supports(java.lang.Class<?> clazz)
clazz
- the class to test for supporttrue
if supported; false
otherwise@Nullable protected java.lang.Object convertFromInternal(Message<?> message, java.lang.Class<?> targetClass, @Nullable java.lang.Object conversionHint)
message
- the input messagetargetClass
- the target class for the conversionconversionHint
- an extra object passed to the MessageConverter
,
e.g. the associated MethodParameter
(may be null
}null
if the converter cannot
perform the conversion@Nullable protected java.lang.Object convertToInternal(java.lang.Object payload, @Nullable MessageHeaders headers, @Nullable java.lang.Object conversionHint)
payload
- the Object to convertheaders
- optional headers for the message (may be null
)conversionHint
- an extra object passed to the MessageConverter
,
e.g. the associated MethodParameter
(may be null
}null
if the converter
cannot perform the conversion