类 MappingJackson2MessageConverter
- java.lang.Object
- org.springframework.jms.support.converter.MappingJackson2MessageConverter
public class MappingJackson2MessageConverter extends Object implements SmartMessageConverter, BeanClassLoaderAware
Message converter that uses Jackson 2.x to convert messages to and from JSON. Maps an object to aBytesMessage
, or to aTextMessage
if thetargetType
is set toMessageType.TEXT
. Converts from aTextMessage
orBytesMessage
to an object.It customizes Jackson's default properties with the following ones:
MapperFeature.DEFAULT_VIEW_INCLUSION
is disabledDeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
is disabled
Compatible with Jackson 2.6 and higher, as of Spring 4.3.
- 从以下版本开始:
- 3.1.4
- 作者:
- Mark Pollack, Dave Syer, Juergen Hoeller, Stephane Nicoll
字段概要
字段 修饰符和类型 字段 说明 static String
DEFAULT_ENCODING
The default encoding used for writing to text messages: UTF-8.
构造器概要
构造器 构造器 说明 MappingJackson2MessageConverter()
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 protected Object
convertFromBytesMessage(BytesMessage message, JavaType targetJavaType)
Convert a BytesMessage to a Java Object with the specified type.protected Object
convertFromMessage(Message message, JavaType targetJavaType)
Template method that allows for custom message mapping.protected Object
convertFromTextMessage(TextMessage message, JavaType targetJavaType)
Convert a TextMessage to a Java Object with the specified type.Object
fromMessage(Message message)
Convert from a JMS Message to a Java object.protected JavaType
getJavaTypeForMessage(Message message)
Determine a Jackson JavaType for the given JMS Message, typically parsing a type id message property.protected Class<?>
getSerializationView(Object conversionHint)
Determine a Jackson serialization view based on the given conversion hint.protected BytesMessage
mapToBytesMessage(Object object, Session session, ObjectMapper objectMapper)
已过时。as of 4.3, usemapToBytesMessage(Object, Session, ObjectWriter)
protected BytesMessage
mapToBytesMessage(Object object, Session session, ObjectWriter objectWriter)
Map the given object to aBytesMessage
.protected Message
mapToMessage(Object object, Session session, ObjectMapper objectMapper, MessageType targetType)
已过时。as of 4.3, usemapToMessage(Object, Session, ObjectWriter, MessageType)
protected Message
mapToMessage(Object object, Session session, ObjectWriter objectWriter, MessageType targetType)
Template method that allows for custom message mapping.protected TextMessage
mapToTextMessage(Object object, Session session, ObjectMapper objectMapper)
已过时。as of 4.3, usemapToTextMessage(Object, Session, ObjectWriter)
protected TextMessage
mapToTextMessage(Object object, Session session, ObjectWriter objectWriter)
Map the given object to aTextMessage
.void
setBeanClassLoader(ClassLoader classLoader)
Callback that supplies the beanclass loader
to a bean instance.void
setEncoding(String encoding)
Specify the encoding to use when converting to and from text-based message body content.void
setEncodingPropertyName(String encodingPropertyName)
Specify the name of the JMS message property that carries the encoding from bytes to String and back is BytesMessage is used during the conversion process.void
setObjectMapper(ObjectMapper objectMapper)
Specify theObjectMapper
to use instead of using the default.void
setTargetType(MessageType targetType)
void
setTypeIdMappings(Map<String,Class<?>> typeIdMappings)
Specify mappings from type ids to Java classes, if desired.protected void
setTypeIdOnMessage(Object object, Message message)
Set a type id for the given payload object on the given JMS Message.void
setTypeIdPropertyName(String typeIdPropertyName)
Specify the name of the JMS message property that carries the type id for the contained object: either a mapped id value or a raw Java class name.Message
toMessage(Object object, Session session)
Convert a Java object to a JMS Message using the supplied session to create the message object.protected Message
toMessage(Object object, Session session, ObjectWriter objectWriter)
Message
toMessage(Object object, Session session, Class<?> jsonView)
Convert a Java object to a JMS Message using the specified json view and the supplied session to create the message object.Message
toMessage(Object object, Session session, Object conversionHint)
A variant ofMessageConverter.toMessage(Object, Session)
which takes an extra conversion context as an argument, allowing to take e.g. annotations on a payload parameter into account.
字段详细资料
DEFAULT_ENCODING
public static final String DEFAULT_ENCODING
The default encoding used for writing to text messages: UTF-8.- 另请参阅:
- 常量字段值
构造器详细资料
MappingJackson2MessageConverter
public MappingJackson2MessageConverter()
方法详细资料
setObjectMapper
public void setObjectMapper(ObjectMapper objectMapper)
Specify theObjectMapper
to use instead of using the default.
setTargetType
public void setTargetType(MessageType targetType)
Specify whethertoMessage(Object, Session)
should marshal to aBytesMessage
or aTextMessage
.The default is
MessageType.BYTES
, i.e. this converter marshals to aBytesMessage
. Note that the default version of this converter supportsMessageType.BYTES
andMessageType.TEXT
only.
setEncoding
public void setEncoding(String encoding)
Specify the encoding to use when converting to and from text-based message body content. The default encoding will be "UTF-8".When reading from a text-based message, an encoding may have been suggested through a special JMS property which will then be preferred over the encoding set on this MessageConverter instance.
setEncodingPropertyName
public void setEncodingPropertyName(String encodingPropertyName)
Specify the name of the JMS message property that carries the encoding from bytes to String and back is BytesMessage is used during the conversion process.Default is none. Setting this property is optional; if not set, UTF-8 will be used for decoding any incoming bytes message.
setTypeIdPropertyName
public void setTypeIdPropertyName(String typeIdPropertyName)
Specify the name of the JMS message property that carries the type id for the contained object: either a mapped id value or a raw Java class name.Default is none. NOTE: This property needs to be set in order to allow for converting from an incoming message to a Java object.
setTypeIdMappings
public void setTypeIdMappings(Map<String,Class<?>> typeIdMappings)
Specify mappings from type ids to Java classes, if desired. This allows for synthetic ids in the type id message property, instead of transferring Java class names.Default is no custom mappings, i.e. transferring raw Java class names.
- 参数:
typeIdMappings
- a Map with type id values as keys and Java classes as values
setBeanClassLoader
public void setBeanClassLoader(ClassLoader classLoader)
从接口复制的说明:BeanClassLoaderAware
Callback that supplies the beanclass loader
to a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as
InitializingBean's
InitializingBean.afterPropertiesSet()
method or a custom init-method.- 指定者:
setBeanClassLoader
在接口中BeanClassLoaderAware
- 参数:
classLoader
- the owning class loader; may benull
in which case a defaultClassLoader
must be used, for example theClassLoader
obtained viaClassUtils.getDefaultClassLoader()
toMessage
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException
从接口复制的说明:MessageConverter
Convert a Java object to a JMS Message using the supplied session to create the message object.- 指定者:
toMessage
在接口中MessageConverter
- 参数:
object
- the object to convertsession
- the Session to use for creating a JMS Message- 返回:
- the JMS Message
- 抛出:
JMSException
- if thrown by JMS API methodsMessageConversionException
- in case of conversion failure
toMessage
public Message toMessage(Object object, Session session, Object conversionHint) throws JMSException, MessageConversionException
从接口复制的说明:SmartMessageConverter
A variant ofMessageConverter.toMessage(Object, Session)
which takes an extra conversion context as an argument, allowing to take e.g. annotations on a payload parameter into account.- 指定者:
toMessage
在接口中SmartMessageConverter
- 参数:
object
- the object to convertsession
- the Session to use for creating a JMS MessageconversionHint
- an extra object passed to theMessageConverter
, e.g. the associatedMethodParameter
(may benull
}- 返回:
- the JMS Message
- 抛出:
JMSException
- if thrown by JMS API methodsMessageConversionException
- in case of conversion failure- 另请参阅:
MessageConverter.toMessage(Object, Session)
toMessage
public Message toMessage(Object object, Session session, Class<?> jsonView) throws JMSException, MessageConversionException
Convert a Java object to a JMS Message using the specified json view and the supplied session to create the message object.- 参数:
object
- the object to convertsession
- the Session to use for creating a JMS MessagejsonView
- the view to use to filter the content- 返回:
- the JMS Message
- 抛出:
JMSException
- if thrown by JMS API methodsMessageConversionException
- in case of conversion failure- 从以下版本开始:
- 4.3
fromMessage
public Object fromMessage(Message message) throws JMSException, MessageConversionException
从接口复制的说明:MessageConverter
Convert from a JMS Message to a Java object.- 指定者:
fromMessage
在接口中MessageConverter
- 参数:
message
- the message to convert- 返回:
- the converted Java object
- 抛出:
JMSException
- if thrown by JMS API methodsMessageConversionException
- in case of conversion failure
toMessage
protected Message toMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, MessageConversionException
mapToTextMessage
@Deprecated protected TextMessage mapToTextMessage(Object object, Session session, ObjectMapper objectMapper) throws JMSException, IOException
已过时。as of 4.3, usemapToTextMessage(Object, Session, ObjectWriter)
Map the given object to aTextMessage
.- 参数:
object
- the object to be mappedsession
- current JMS sessionobjectMapper
- the mapper to use- 返回:
- the resulting message
- 抛出:
JMSException
- if thrown by JMS methodsIOException
- in case of I/O errors- 另请参阅:
Session.createBytesMessage()
mapToTextMessage
protected TextMessage mapToTextMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, IOException
Map the given object to aTextMessage
.- 参数:
object
- the object to be mappedsession
- current JMS sessionobjectWriter
- the writer to use- 返回:
- the resulting message
- 抛出:
JMSException
- if thrown by JMS methodsIOException
- in case of I/O errors- 从以下版本开始:
- 4.3
- 另请参阅:
Session.createBytesMessage()
mapToBytesMessage
@Deprecated protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectMapper objectMapper) throws JMSException, IOException
已过时。as of 4.3, usemapToBytesMessage(Object, Session, ObjectWriter)
Map the given object to aBytesMessage
.- 参数:
object
- the object to be mappedsession
- current JMS sessionobjectMapper
- the mapper to use- 返回:
- the resulting message
- 抛出:
JMSException
- if thrown by JMS methodsIOException
- in case of I/O errors- 另请参阅:
Session.createBytesMessage()
mapToBytesMessage
protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, IOException
Map the given object to aBytesMessage
.- 参数:
object
- the object to be mappedsession
- current JMS sessionobjectWriter
- the writer to use- 返回:
- the resulting message
- 抛出:
JMSException
- if thrown by JMS methodsIOException
- in case of I/O errors- 从以下版本开始:
- 4.3
- 另请参阅:
Session.createBytesMessage()
mapToMessage
@Deprecated protected Message mapToMessage(Object object, Session session, ObjectMapper objectMapper, MessageType targetType) throws JMSException, IOException
已过时。as of 4.3, usemapToMessage(Object, Session, ObjectWriter, MessageType)
Template method that allows for custom message mapping. Invoked whensetTargetType(org.springframework.jms.support.converter.MessageType)
is notMessageType.TEXT
orMessageType.BYTES
.The default implementation throws an
IllegalArgumentException
.- 参数:
object
- the object to marshalsession
- the JMS SessionobjectMapper
- the mapper to usetargetType
- the target message type (other than TEXT or BYTES)- 返回:
- the resulting message
- 抛出:
JMSException
- if thrown by JMS methodsIOException
- in case of I/O errors
mapToMessage
protected Message mapToMessage(Object object, Session session, ObjectWriter objectWriter, MessageType targetType) throws JMSException, IOException
Template method that allows for custom message mapping. Invoked whensetTargetType(org.springframework.jms.support.converter.MessageType)
is notMessageType.TEXT
orMessageType.BYTES
.The default implementation throws an
IllegalArgumentException
.- 参数:
object
- the object to marshalsession
- the JMS SessionobjectWriter
- the writer to usetargetType
- the target message type (other than TEXT or BYTES)- 返回:
- the resulting message
- 抛出:
JMSException
- if thrown by JMS methodsIOException
- in case of I/O errors
setTypeIdOnMessage
protected void setTypeIdOnMessage(Object object, Message message) throws JMSException
Set a type id for the given payload object on the given JMS Message.The default implementation consults the configured type id mapping and sets the resulting value (either a mapped id or the raw Java class name) into the configured type id message property.
- 参数:
object
- the payload object to set a type id formessage
- the JMS Message on which to set the type id property- 抛出:
JMSException
- if thrown by JMS methods- 另请参阅:
getJavaTypeForMessage(javax.jms.Message)
,setTypeIdPropertyName(String)
,setTypeIdMappings(java.util.Map)
convertFromTextMessage
protected Object convertFromTextMessage(TextMessage message, JavaType targetJavaType) throws JMSException, IOException
Convert a TextMessage to a Java Object with the specified type.- 参数:
message
- the input messagetargetJavaType
- the target type- 返回:
- the message converted to an object
- 抛出:
JMSException
- if thrown by JMSIOException
- in case of I/O errors
convertFromBytesMessage
protected Object convertFromBytesMessage(BytesMessage message, JavaType targetJavaType) throws JMSException, IOException
Convert a BytesMessage to a Java Object with the specified type.- 参数:
message
- the input messagetargetJavaType
- the target type- 返回:
- the message converted to an object
- 抛出:
JMSException
- if thrown by JMSIOException
- in case of I/O errors
convertFromMessage
protected Object convertFromMessage(Message message, JavaType targetJavaType) throws JMSException, IOException
Template method that allows for custom message mapping. Invoked whensetTargetType(org.springframework.jms.support.converter.MessageType)
is notMessageType.TEXT
orMessageType.BYTES
.The default implementation throws an
IllegalArgumentException
.- 参数:
message
- the input messagetargetJavaType
- the target type- 返回:
- the message converted to an object
- 抛出:
JMSException
- if thrown by JMSIOException
- in case of I/O errors
getJavaTypeForMessage
protected JavaType getJavaTypeForMessage(Message message) throws JMSException
Determine a Jackson JavaType for the given JMS Message, typically parsing a type id message property.The default implementation parses the configured type id property name and consults the configured type id mapping. This can be overridden with a different strategy, e.g. doing some heuristics based on message origin.
- 参数:
message
- the JMS Message from which to get the type id property- 抛出:
JMSException
- if thrown by JMS methods- 另请参阅:
setTypeIdOnMessage(Object, javax.jms.Message)
,setTypeIdPropertyName(String)
,setTypeIdMappings(java.util.Map)
getSerializationView
protected Class<?> getSerializationView(Object conversionHint)
Determine a Jackson serialization view based on the given conversion hint.- 参数:
conversionHint
- the conversion hint Object as passed into the converter for the current conversion attempt- 返回:
- the serialization view class, or
null
if none