public class MappingJackson2MessageConverter extends java.lang.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 |
---|---|
private java.lang.ClassLoader |
beanClassLoader |
private java.util.Map<java.lang.Class<?>,java.lang.String> |
classIdMappings |
static java.lang.String |
DEFAULT_ENCODING
The default encoding used for writing to text messages: UTF-8.
|
private java.lang.String |
encoding |
private java.lang.String |
encodingPropertyName |
private java.util.Map<java.lang.String,java.lang.Class<?>> |
idClassMappings |
private ObjectMapper |
objectMapper |
private MessageType |
targetType |
private java.lang.String |
typeIdPropertyName |
Constructor and Description |
---|
MappingJackson2MessageConverter() |
Modifier and Type | Method and Description |
---|---|
protected java.lang.Object |
convertFromBytesMessage(BytesMessage message,
JavaType targetJavaType)
Convert a BytesMessage to a Java Object with the specified type.
|
protected java.lang.Object |
convertFromMessage(Message message,
JavaType targetJavaType)
Template method that allows for custom message mapping.
|
protected java.lang.Object |
convertFromTextMessage(TextMessage message,
JavaType targetJavaType)
Convert a TextMessage to a Java Object with the specified type.
|
private java.lang.Object |
convertToObject(Message message,
JavaType targetJavaType)
Convenience method to dispatch to converters for individual message types.
|
java.lang.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 BytesMessage |
mapToBytesMessage(java.lang.Object object,
Session session,
ObjectMapper objectMapper)
Map the given object to a
BytesMessage . |
protected Message |
mapToMessage(java.lang.Object object,
Session session,
ObjectMapper objectMapper,
MessageType targetType)
Template method that allows for custom message mapping.
|
protected TextMessage |
mapToTextMessage(java.lang.Object object,
Session session,
ObjectMapper objectMapper)
Map the given object to a
TextMessage . |
void |
setBeanClassLoader(java.lang.ClassLoader classLoader)
Callback that supplies the bean <code>class loader</code> to
a bean instance.
|
void |
setEncoding(java.lang.String encoding)
Specify the encoding to use when converting to and from text-based
message body content.
|
void |
setEncodingPropertyName(java.lang.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 the
ObjectMapper to use instead of using the default. |
void |
setTargetType(MessageType targetType)
|
void |
setTypeIdMappings(java.util.Map<java.lang.String,java.lang.Class<?>> typeIdMappings)
Specify mappings from type ids to Java classes, if desired.
|
protected void |
setTypeIdOnMessage(java.lang.Object object,
Message message)
Set a type id for the given payload object on the given JMS Message.
|
void |
setTypeIdPropertyName(java.lang.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(java.lang.Object object,
Session session)
Convert a Java object to a JMS Message using the supplied session
to create the message object.
|
public static final java.lang.String DEFAULT_ENCODING
private ObjectMapper objectMapper
private MessageType targetType
private java.lang.String encoding
private java.lang.String encodingPropertyName
private java.lang.String typeIdPropertyName
private java.util.Map<java.lang.String,java.lang.Class<?>> idClassMappings
private java.util.Map<java.lang.Class<?>,java.lang.String> classIdMappings
private java.lang.ClassLoader beanClassLoader
public void setObjectMapper(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(java.lang.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(java.lang.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(java.lang.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(java.util.Map<java.lang.String,java.lang.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(java.lang.ClassLoader classLoader)
BeanClassLoaderAware
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(java.lang.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 MessageMessageConversionException
- in case of conversion failureJMSException
public java.lang.Object fromMessage(Message message) throws JMSException, MessageConversionException
MessageConverter
fromMessage
in interface MessageConverter
message
- the message to convertMessageConversionException
- in case of conversion failureJMSException
protected TextMessage mapToTextMessage(java.lang.Object object, Session session, ObjectMapper objectMapper) throws JMSException, java.io.IOException
TextMessage
.object
- the object to be mappedsession
- current JMS sessionobjectMapper
- the mapper to useJMSException
- if thrown by JMS methodsjava.io.IOException
- in case of I/O errorsSession#createBytesMessage
protected BytesMessage mapToBytesMessage(java.lang.Object object, Session session, ObjectMapper objectMapper) throws JMSException, java.io.IOException
BytesMessage
.object
- the object to be mappedsession
- current JMS sessionobjectMapper
- the mapper to useJMSException
- if thrown by JMS methodsjava.io.IOException
- in case of I/O errorsSession#createBytesMessage
protected Message mapToMessage(java.lang.Object object, Session session, ObjectMapper objectMapper, MessageType targetType) throws JMSException, java.io.IOException
setTargetType(org.springframework.jms.support.converter.MessageType)
is not MessageType.TEXT
or
MessageType.BYTES
.
The default implementation throws an <code>IllegalArgumentException</code>.
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 methodsjava.io.IOException
- in case of I/O errorsprotected void setTypeIdOnMessage(java.lang.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 methods#getJavaTypeForMessage(javax.jms.Message)
,
setTypeIdPropertyName(String)
,
setTypeIdMappings(java.util.Map)
private java.lang.Object convertToObject(Message message, JavaType targetJavaType) throws JMSException, java.io.IOException
JMSException
java.io.IOException
protected java.lang.Object convertFromTextMessage(TextMessage message, JavaType targetJavaType) throws JMSException, java.io.IOException
message
- the input messagetargetJavaType
- the target typeJMSException
- if thrown by JMSjava.io.IOException
- in case of I/O errorsprotected java.lang.Object convertFromBytesMessage(BytesMessage message, JavaType targetJavaType) throws JMSException, java.io.IOException
message
- the input messagetargetJavaType
- the target typeJMSException
- if thrown by JMSjava.io.IOException
- in case of I/O errorsprotected java.lang.Object convertFromMessage(Message message, JavaType targetJavaType) throws JMSException, java.io.IOException
setTargetType(org.springframework.jms.support.converter.MessageType)
is not MessageType.TEXT
or
MessageType.BYTES
.
The default implementation throws an <code>IllegalArgumentException</code>.
message
- the input messagetargetJavaType
- the target typeJMSException
- if thrown by JMSjava.io.IOException
- in case of I/O errorsprotected 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 methods#setTypeIdOnMessage(Object, javax.jms.Message)
,
setTypeIdPropertyName(String)
,
setTypeIdMappings(java.util.Map)