public class MappingJackson2MessageConverter extends Object implements MessageConverter, BeanClassLoaderAware
BytesMessage
, or to a TextMessage
if the
targetType
is set to MessageType.TEXT
.
Converts from a TextMessage
or BytesMessage
to an object.Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_ENCODING
The default encoding used for writing to text messages: UTF-8.
|
Constructor and Description |
---|
MappingJackson2MessageConverter() |
Modifier and Type | Method and Description |
---|---|
protected Object |
convertFromBytesMessage(BytesMessage message,
com.fasterxml.jackson.databind.JavaType targetJavaType)
Convert a BytesMessage to a Java Object with the specified type.
|
protected Object |
convertFromMessage(Message message,
com.fasterxml.jackson.databind.JavaType targetJavaType)
Template method that allows for custom message mapping.
|
protected Object |
convertFromTextMessage(TextMessage message,
com.fasterxml.jackson.databind.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 com.fasterxml.jackson.databind.JavaType |
getJavaTypeForMessage(Message message)
Determine a Jackson JavaType for the given JMS Message,
typically parsing a type id message property.
|
protected BytesMessage |
mapToBytesMessage(Object object,
Session session,
com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Map the given object to a
BytesMessage . |
protected Message |
mapToMessage(Object object,
Session session,
com.fasterxml.jackson.databind.ObjectMapper objectMapper,
MessageType targetType)
Template method that allows for custom message mapping.
|
protected TextMessage |
mapToTextMessage(Object object,
Session session,
com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Map the given object to a
TextMessage . |
void |
setBeanClassLoader(ClassLoader classLoader)
Callback that supplies the bean
class 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(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Specify the
ObjectMapper 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.
|
public static final String DEFAULT_ENCODING
public void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
ObjectMapper
to use instead of using the default.public void setTargetType(MessageType targetType)
toMessage(Object, Session)
should marshal to a
BytesMessage
or a TextMessage
.
The default is MessageType.BYTES
, i.e. this converter marshals to
a BytesMessage
. Note that the default version of this converter
supports MessageType.BYTES
and MessageType.TEXT
only.
MessageType.BYTES
,
MessageType.TEXT
public void setEncoding(String encoding)
When reading from a 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.
public void setEncodingPropertyName(String encodingPropertyName)
Default is none. Setting this property is optional; if not set, UTF-8 will be used for decoding any incoming bytes message.
setEncoding(java.lang.String)
public void setTypeIdPropertyName(String typeIdPropertyName)
Default is none. NOTE: This property needs to be set in order to allow for converting from an incoming message to a Java object.
public void setTypeIdMappings(Map<String,Class<?>> typeIdMappings)
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 valuespublic void setBeanClassLoader(ClassLoader classLoader)
BeanClassLoaderAware
class 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
in interface BeanClassLoaderAware
classLoader
- the owning class loader; may be null
in
which case a default ClassLoader
must be used, for example
the ClassLoader
obtained via
ClassUtils.getDefaultClassLoader()
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException
MessageConverter
toMessage
in interface MessageConverter
object
- the object to convertsession
- the Session to use for creating a JMS MessageJMSException
- if thrown by JMS API methodsMessageConversionException
- in case of conversion failurepublic Object fromMessage(Message message) throws JMSException, MessageConversionException
MessageConverter
fromMessage
in interface MessageConverter
message
- the message to convertJMSException
- if thrown by JMS API methodsMessageConversionException
- in case of conversion failureprotected TextMessage mapToTextMessage(Object object, Session session, com.fasterxml.jackson.databind.ObjectMapper objectMapper) throws JMSException, IOException
TextMessage
.object
- the object to be mappedsession
- current JMS sessionobjectMapper
- the mapper to useJMSException
- if thrown by JMS methodsIOException
- in case of I/O errorsSession.createBytesMessage()
protected BytesMessage mapToBytesMessage(Object object, Session session, com.fasterxml.jackson.databind.ObjectMapper objectMapper) throws JMSException, IOException
BytesMessage
.object
- the object to be mappedsession
- current JMS sessionobjectMapper
- the mapper to useJMSException
- if thrown by JMS methodsIOException
- in case of I/O errorsSession.createBytesMessage()
protected Message mapToMessage(Object object, Session session, com.fasterxml.jackson.databind.ObjectMapper objectMapper, MessageType targetType) throws JMSException, IOException
setTargetType(org.springframework.jms.support.converter.MessageType)
is not MessageType.TEXT
or
MessageType.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)JMSException
- if thrown by JMS methodsIOException
- in case of I/O errorsprotected void setTypeIdOnMessage(Object object, Message message) throws JMSException
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 to set the type id onJMSException
- if thrown by JMS methodsgetJavaTypeForMessage(javax.jms.Message)
,
setTypeIdPropertyName(String)
,
setTypeIdMappings(java.util.Map)
protected Object convertFromTextMessage(TextMessage message, com.fasterxml.jackson.databind.JavaType targetJavaType) throws JMSException, IOException
message
- the input messagetargetJavaType
- the target typeJMSException
- if thrown by JMSIOException
- in case of I/O errorsprotected Object convertFromBytesMessage(BytesMessage message, com.fasterxml.jackson.databind.JavaType targetJavaType) throws JMSException, IOException
message
- the input messagetargetJavaType
- the target typeJMSException
- if thrown by JMSIOException
- in case of I/O errorsprotected Object convertFromMessage(Message message, com.fasterxml.jackson.databind.JavaType targetJavaType) throws JMSException, IOException
setTargetType(org.springframework.jms.support.converter.MessageType)
is not MessageType.TEXT
or
MessageType.BYTES
.
The default implementation throws an IllegalArgumentException
.
message
- the input messagetargetJavaType
- the target typeJMSException
- if thrown by JMSIOException
- in case of I/O errorsprotected com.fasterxml.jackson.databind.JavaType getJavaTypeForMessage(Message message) throws JMSException
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 to set the type id onJMSException
- if thrown by JMS methodssetTypeIdOnMessage(Object, javax.jms.Message)
,
setTypeIdPropertyName(String)
,
setTypeIdMappings(java.util.Map)