public abstract class AbstractMessageConverter extends Object implements MessageConverter
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.| Modifier | Constructor and Description |
|---|---|
protected |
AbstractMessageConverter(Collection<MimeType> supportedMimeTypes)
Construct an
AbstractMessageConverter with multiple supported MIME type. |
protected |
AbstractMessageConverter(MimeType supportedMimeType)
Construct an
AbstractMessageConverter with one supported MIME type. |
| 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 default
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 configured supported MIME types.
|
void |
setContentTypeResolver(ContentTypeResolver resolver)
Configure the
ContentTypeResolver to use. |
void |
setSerializedPayloadClass(Class<?> payloadClass)
Configure the preferred serialization class to use (byte[] or String) when
converting an Object payload to a
Message. |
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. |
protected final Log logger
protected AbstractMessageConverter(MimeType supportedMimeType)
AbstractMessageConverter with one supported MIME type.supportedMimeType - the supported MIME typeprotected AbstractMessageConverter(Collection<MimeType> supportedMimeTypes)
AbstractMessageConverter with multiple supported MIME type.supportedMimeTypes - the supported MIME typespublic List<MimeType> getSupportedMimeTypes()
public void setContentTypeResolver(ContentTypeResolver resolver)
ContentTypeResolver to use.
The default value is null. However when CompositeMessageConverter
is used it configures all of its delegates with a default resolver.
public ContentTypeResolver getContentTypeResolver()
ContentTypeResolver.public void setSerializedPayloadClass(Class<?> payloadClass)
Message.
The default value is byte[].
payloadClass - either byte[] or Stringpublic Class<?> getSerializedPayloadClass()
protected MimeType getDefaultContentType(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 knownprotected abstract boolean supports(Class<?> clazz)
clazz - the class to test for supporttrue if supported; false otherwisepublic final Object fromMessage(Message<?> message, Class<?> targetClass)
MessageConverterMessage 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.
fromMessage in interface MessageConvertermessage - the input messagetargetClass - the target class for the conversionnull if the converter cannot
perform the conversionpublic abstract Object convertFromInternal(Message<?> message, Class<?> targetClass)
public final Message<?> toMessage(Object payload, MessageHeaders headers)
MessageConverterMessage 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 MessageConverterpayload - the Object to convertheaders - optional headers for the message, may be nullnull if the converter does not support the
Object type or the target media typeprotected boolean canConvertTo(Object payload, MessageHeaders headers)
public abstract Object convertToInternal(Object payload, MessageHeaders headers)
protected boolean supportsMimeType(MessageHeaders headers)
protected MimeType getMimeType(MessageHeaders headers)