public class MappingJackson2MessageConverter extends 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 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,
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)
Deprecated.
as of 4.3, use
mapToBytesMessage(Object, Session, ObjectWriter) |
protected BytesMessage |
mapToBytesMessage(Object object,
Session session,
ObjectWriter objectWriter)
Map the given object to a
BytesMessage . |
protected Message |
mapToMessage(Object object,
Session session,
ObjectMapper objectMapper,
MessageType targetType)
Deprecated.
as of 4.3, use
mapToMessage(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)
Deprecated.
as of 4.3, use
mapToTextMessage(Object, Session, ObjectWriter) |
protected TextMessage |
mapToTextMessage(Object object,
Session session,
ObjectWriter objectWriter)
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(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.
|
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 of
MessageConverter.toMessage(Object, Session) which takes an extra conversion
context as an argument, allowing to take e.g. |
protected Message |
toMessage(Object object,
Session session,
ObjectWriter objectWriter) |
public static final 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(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 Message toMessage(Object object, Session session, 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(Object object, Session session, 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 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(Object object, Session session, ObjectWriter objectWriter) throws JMSException, MessageConversionException
@Deprecated protected TextMessage mapToTextMessage(Object object, Session session, ObjectMapper objectMapper) throws JMSException, IOException
mapToTextMessage(Object, Session, ObjectWriter)
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 TextMessage mapToTextMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, IOException
TextMessage
.object
- the object to be mappedsession
- current JMS sessionobjectWriter
- the writer to useJMSException
- if thrown by JMS methodsIOException
- in case of I/O errorsSession.createBytesMessage()
@Deprecated protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectMapper objectMapper) throws JMSException, IOException
mapToBytesMessage(Object, Session, ObjectWriter)
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 BytesMessage mapToBytesMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, IOException
BytesMessage
.object
- the object to be mappedsession
- current JMS sessionobjectWriter
- the writer to useJMSException
- if thrown by JMS methodsIOException
- in case of I/O errorsSession.createBytesMessage()
@Deprecated protected Message mapToMessage(Object object, Session session, ObjectMapper objectMapper, MessageType targetType) throws JMSException, IOException
mapToMessage(Object, Session, ObjectWriter, MessageType)
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 Message mapToMessage(Object object, Session session, ObjectWriter objectWriter, 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 SessionobjectWriter
- the writer 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, 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, 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, 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 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)
protected Class<?> getSerializationView(Object conversionHint)
conversionHint
- the conversion hint Object as passed into the
converter for the current conversion attemptnull
if none