类 AbstractMessageConverter
- java.lang.Object
- org.springframework.messaging.converter.AbstractMessageConverter
- 所有已实现的接口:
MessageConverter
,SmartMessageConverter
- 直接已知子类:
ByteArrayMessageConverter
,MappingJackson2MessageConverter
,MarshallingMessageConverter
,StringMessageConverter
public abstract class AbstractMessageConverter extends Object implements SmartMessageConverter
Abstract base class forSmartMessageConverter
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.- 从以下版本开始:
- 4.0
- 作者:
- Rossen Stoyanchev, Sebastien Deleuze, Juergen Hoeller
构造器概要
构造器 限定符 构造器 说明 protected
AbstractMessageConverter(Collection<MimeType> supportedMimeTypes)
Construct anAbstractMessageConverter
supporting multiple MIME types.protected
AbstractMessageConverter(MimeType supportedMimeType)
Construct anAbstractMessageConverter
supporting a single MIME type.
方法概要
所有方法 实例方法 抽象方法 具体方法 已过时的方法 修饰符和类型 方法 说明 protected boolean
canConvertFrom(Message<?> message, Class<?> targetClass)
protected boolean
canConvertTo(Object payload, MessageHeaders headers)
Object
convertFromInternal(Message<?> message, Class<?> targetClass)
已过时。as of Spring 4.2, in favor ofconvertFromInternal(Message, Class, Object)
(which is also protected instead of public)protected Object
convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint)
Convert the message payload from serialized form to an Object.Object
convertToInternal(Object payload, MessageHeaders headers)
已过时。as of Spring 4.2, in favor ofconvertFromInternal(Message, Class, Object)
(which is also protected instead of public)protected Object
convertToInternal(Object payload, MessageHeaders headers, Object conversionHint)
Convert the payload object to serialized form.Object
fromMessage(Message<?> message, Class<?> targetClass)
Convert the payload of aMessage
from a serialized form to a typed Object of the specified target class.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. annotations on a payload parameter into account.ContentTypeResolver
getContentTypeResolver()
Return the configuredContentTypeResolver
.protected MimeType
getDefaultContentType(Object payload)
Return 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 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 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
supports(Class<?> clazz)
Whether the given class is supported by this converter.protected boolean
supportsMimeType(MessageHeaders headers)
Message<?>
toMessage(Object payload, MessageHeaders headers)
Create aMessage
whose payload is the result of converting the given payload Object to serialized form.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. annotations on a return type into account.
构造器详细资料
AbstractMessageConverter
protected AbstractMessageConverter(MimeType supportedMimeType)
Construct anAbstractMessageConverter
supporting a single MIME type.- 参数:
supportedMimeType
- the supported MIME type
AbstractMessageConverter
protected AbstractMessageConverter(Collection<MimeType> supportedMimeTypes)
Construct anAbstractMessageConverter
supporting multiple MIME types.- 参数:
supportedMimeTypes
- the supported MIME types
方法详细资料
getSupportedMimeTypes
public List<MimeType> getSupportedMimeTypes()
Return the supported MIME types.
setContentTypeResolver
public void setContentTypeResolver(ContentTypeResolver resolver)
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
public ContentTypeResolver 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 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
public void setSerializedPayloadClass(Class<?> payloadClass)
Configure the preferred serialization class to use (byte[] or String) when converting an Object payload to aMessage
.The default value is byte[].
- 参数:
payloadClass
- either byte[] or String
getSerializedPayloadClass
public Class<?> getSerializedPayloadClass()
Return the configured preferred serialization payload class.
fromMessage
public final Object fromMessage(Message<?> message, Class<?> targetClass)
从接口复制的说明: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
.- 指定者:
fromMessage
在接口中MessageConverter
- 参数:
message
- the input messagetargetClass
- the target class for the conversion- 返回:
- the result of the conversion, or
null
if the converter cannot perform the conversion
fromMessage
public final Object fromMessage(Message<?> message, Class<?> targetClass, Object conversionHint)
从接口复制的说明: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.- 指定者:
fromMessage
在接口中SmartMessageConverter
- 参数:
message
- the input messagetargetClass
- the target class for the conversionconversionHint
- an extra object passed to theMessageConverter
, e.g. the associatedMethodParameter
(may benull
}- 返回:
- the result of the conversion, or
null
if the converter cannot perform the conversion - 另请参阅:
MessageConverter.fromMessage(Message, Class)
toMessage
public final Message<?> toMessage(Object payload, MessageHeaders headers)
从接口复制的说明: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
.- 指定者:
toMessage
在接口中MessageConverter
- 参数:
payload
- the Object to convertheaders
- optional headers for the message (may benull
)- 返回:
- the new message, or
null
if the converter does not support the Object type or the target media type
toMessage
public final Message<?> toMessage(Object payload, MessageHeaders headers, Object conversionHint)
从接口复制的说明: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.- 指定者:
toMessage
在接口中SmartMessageConverter
- 参数:
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
}- 返回:
- the new message, or
null
if the converter does not support the Object type or the target media type - 另请参阅:
MessageConverter.toMessage(Object, MessageHeaders)
canConvertFrom
protected boolean canConvertFrom(Message<?> message, Class<?> targetClass)
canConvertTo
protected boolean canConvertTo(Object payload, MessageHeaders headers)
supportsMimeType
protected boolean supportsMimeType(MessageHeaders headers)
getMimeType
protected MimeType getMimeType(MessageHeaders headers)
getDefaultContentType
protected MimeType getDefaultContentType(Object payload)
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.- 参数:
payload
- the payload being converted to a message- 返回:
- the content type, or
null
if not known
supports
protected abstract boolean supports(Class<?> clazz)
Whether the given class is supported by this converter.- 参数:
clazz
- the class to test for support- 返回:
true
if supported;false
otherwise
convertFromInternal
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint)
Convert the message payload from serialized form to an Object.- 参数:
message
- the input messagetargetClass
- the target class for the conversionconversionHint
- an extra object passed to theMessageConverter
, e.g. the associatedMethodParameter
(may benull
}- 返回:
- the result of the conversion, or
null
if the converter cannot perform the conversion - 从以下版本开始:
- 4.2
convertToInternal
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint)
Convert the payload object to serialized form.- 参数:
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
}- 返回:
- the resulting payload for the message, or
null
if the converter cannot perform the conversion - 从以下版本开始:
- 4.2
convertFromInternal
@Deprecated public Object convertFromInternal(Message<?> message, Class<?> targetClass)
已过时。as of Spring 4.2, in favor ofconvertFromInternal(Message, Class, Object)
(which is also protected instead of public)Convert the message payload from serialized form to an Object.
convertToInternal
@Deprecated public Object convertToInternal(Object payload, MessageHeaders headers)
已过时。as of Spring 4.2, in favor ofconvertFromInternal(Message, Class, Object)
(which is also protected instead of public)Convert the payload object to serialized form.