public class MappingJackson2MessageConverter extends java.lang.Object implements SmartMessageConverter, BeanClassLoaderAware
BytesMessage
, or to a TextMessage
if the
targetType
is set to MessageType.TEXT
.
Converts from a TextMessage
or BytesMessage
to an object.
It customizes Jackson's default properties with the following ones:
MapperFeature.DEFAULT_VIEW_INCLUSION
is disabledDeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
is disabledCompatible with Jackson 2.6 and higher, as of Spring 4.3.
Modifier and Type | Field and Description |
---|---|
static java.lang.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 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.
|
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 java.lang.Class<?> |
getSerializationView(java.lang.Object conversionHint)
Determine a Jackson serialization view based on the given conversion hint.
|
protected BytesMessage |
mapToBytesMessage(java.lang.Object object,
Session session,
ObjectWriter objectWriter)
Map the given object to a
BytesMessage . |
protected Message |
mapToMessage(java.lang.Object object,
Session session,
ObjectWriter objectWriter,
MessageType targetType)
Template method that allows for custom message mapping.
|
protected TextMessage |
mapToTextMessage(java.lang.Object object,
Session session,
ObjectWriter objectWriter)
Map the given object to a
TextMessage . |
void |
setBeanClassLoader(java.lang.ClassLoader classLoader)
Callback that supplies the bean
class loader 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.
|
Message |
toMessage(java.lang.Object object,
Session session,
java.lang.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(java.lang.Object object,
Session session,
java.lang.Object conversionHint)
A variant of
MessageConverter.toMessage(Object, Session) which takes an extra conversion
context as an argument, allowing to take e.g. |
protected Message |
toMessage(java.lang.Object object,
Session session,
ObjectWriter objectWriter) |
public static final java.lang.String DEFAULT_ENCODING
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
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 loaderpublic 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 MessageJMSException
- if thrown by JMS API methodsMessageConversionException
- in case of conversion failurepublic Message toMessage(java.lang.Object object, Session session, @Nullable java.lang.Object conversionHint) throws JMSException, MessageConversionException
SmartMessageConverter
MessageConverter.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
in interface SmartMessageConverter
object
- the object to convertsession
- the Session to use for creating a JMS MessageconversionHint
- an extra object passed to the MessageConverter
,
e.g. the associated MethodParameter
(may be null
}JMSException
- if thrown by JMS API methodsMessageConversionException
- in case of conversion failureMessageConverter.toMessage(Object, Session)
public Message toMessage(java.lang.Object object, Session session, @Nullable java.lang.Class<?> jsonView) throws JMSException, MessageConversionException
object
- the object to convertsession
- the Session to use for creating a JMS MessagejsonView
- the view to use to filter the contentJMSException
- if thrown by JMS API methodsMessageConversionException
- in case of conversion failurepublic java.lang.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 Message toMessage(java.lang.Object object, Session session, ObjectWriter objectWriter) throws JMSException, MessageConversionException
protected TextMessage mapToTextMessage(java.lang.Object object, Session session, ObjectWriter objectWriter) throws JMSException, java.io.IOException
TextMessage
.object
- the object to be mappedsession
- current JMS sessionobjectWriter
- the writer 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, ObjectWriter objectWriter) throws JMSException, java.io.IOException
BytesMessage
.object
- the object to be mappedsession
- current JMS sessionobjectWriter
- the writer 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, ObjectWriter objectWriter, 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 IllegalArgumentException
.
object
- the object to marshalsession
- the JMS SessionobjectWriter
- the writer 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 methodsgetJavaTypeForMessage(javax.jms.Message)
,
setTypeIdPropertyName(String)
,
setTypeIdMappings(java.util.Map)
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 IllegalArgumentException
.
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 methodssetTypeIdOnMessage(Object, javax.jms.Message)
,
setTypeIdPropertyName(String)
,
setTypeIdMappings(java.util.Map)
@Nullable protected java.lang.Class<?> getSerializationView(@Nullable java.lang.Object conversionHint)
conversionHint
- the conversion hint Object as passed into the
converter for the current conversion attemptnull
if none