Top Description Fields Constructors Methods
org.springframework.jms.support.converter

public Class MappingJackson2MessageConverter

extends Object
implements SmartMessageConverter, BeanClassLoaderAware
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.Aware, org.springframework.jms.support.converter.SmartMessageConverter, org.springframework.jms.support.converter.MessageConverter
Imports
java.io.ByteArrayOutputStream, .IOException, .OutputStreamWriter, .StringWriter, .UnsupportedEncodingException, java.util.HashMap, .Map, com.fasterxml.jackson.annotation.JsonView, com.fasterxml.jackson.databind.DeserializationFeature, .JavaType, .MapperFeature, .ObjectMapper, .ObjectWriter, jakarta.jms.BytesMessage, .JMSException, .Message, .Session, .TextMessage, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.core.MethodParameter, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils

Message converter that uses Jackson 2.x to convert messages to and from JSON.

Maps an object to a 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:

Authors
Mark Pollack, Dave Syer, Juergen Hoeller, Stephane Nicoll
Since
3.1.4

Field Summary

Modifier and TypeField and Description
private ClassLoader
private final Map<Class<?>, String>
public static final String
DEFAULT_ENCODING

The default encoding used for writing to text messages: UTF-8.

private String
private String
private Map<String, Class<?>>
private ObjectMapper
private MessageType
private String

Constructor Summary

AccessConstructor and Description
public
MappingJackson2MessageConverter()

Construct a MappingJackson2MessageConverter with a default ObjectMapper.

public
MappingJackson2MessageConverter(ObjectMapper
the ObjectMapper to use
objectMapper
)

Construct a MappingJackson2MessageConverter with a custom ObjectMapper.

Method Summary

Modifier and TypeMethod and Description
protected Object

Returns:

the message converted to an object
convertFromBytesMessage
(BytesMessage
the input message
message
,
JavaType
the target type
targetJavaType
)

Convert a BytesMessage to a Java Object with the specified type.

protected Object

Returns:

the message converted to an object
convertFromMessage
(Message
the input message
message
,
JavaType
the target type
targetJavaType
)

Template method that allows for custom message mapping.

protected Object

Returns:

the message converted to an object
convertFromTextMessage
(TextMessage
the input message
message
,
JavaType
the target type
targetJavaType
)

Convert a TextMessage to a Java Object with the specified type.

private Object
convertToObject(Message message, JavaType targetJavaType)

Convenience method to dispatch to converters for individual message types.

private Class<?>
extractViewClass(JsonView annotation, Object conversionHint)

public Object
fromMessage(Message
the message to convert
message
)

Implements org.springframework.jms.support.converter.MessageConverter.fromMessage.

Convert from a JMS Message to a Java object.

protected JavaType
getJavaTypeForMessage(Message
the JMS Message from which to get the type id property
message
)

Determine a Jackson JavaType for the given JMS Message, typically parsing a type id message property.

protected Class<?>

Returns:

the serialization view class, or null if none
getSerializationView
(Object
the conversion hint Object as passed into the converter for the current conversion attempt
conversionHint
)

Determine a Jackson serialization view based on the given conversion hint.

protected BytesMessage

Returns:

the resulting message
mapToBytesMessage
(Object
the object to be mapped
object
,
Session
current JMS session
session
,
ObjectWriter
the writer to use
objectWriter
)

Map the given object to a BytesMessage.

protected Message

Returns:

the resulting message
mapToMessage
(Object
the object to marshal
object
,
Session
the JMS Session
session
,
ObjectWriter
the writer to use
objectWriter
,
MessageType
the target message type (other than TEXT or BYTES)
targetType
)

Template method that allows for custom message mapping.

protected TextMessage

Returns:

the resulting message
mapToTextMessage
(Object
the object to be mapped
object
,
Session
current JMS session
session
,
ObjectWriter
the writer to use
objectWriter
)

Map the given object to a TextMessage.

public void
setBeanClassLoader(ClassLoader
the owning class loader
classLoader
)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean class loader to a bean instance.

public void
setEncoding(String encoding)

Specify the encoding to use when converting to and from text-based message body content.

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.

public void
setObjectMapper(ObjectMapper objectMapper)

Set the ObjectMapper for this converter.

public void
setTargetType(MessageType targetType)

Specify whether toMessage(Object, Session) should marshal to a BytesMessage or a TextMessage.

public void
setTypeIdMappings(Map<String, Class<?>>
a Map with type id values as keys and Java classes as values
typeIdMappings
)

Specify mappings from type ids to Java classes, if desired.

protected void
setTypeIdOnMessage(Object
the payload object to set a type id for
object
,
Message
the JMS Message on which to set the type id property
message
)

Set a type id for the given payload object on the given JMS Message.

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.

public Message
toMessage(Object
the object to convert
object
,
Session
the Session to use for creating a JMS Message
session
)

Implements org.springframework.jms.support.converter.MessageConverter.toMessage.

Convert a Java object to a JMS Message using the supplied session to create the message object.

public Message
toMessage(Object
the object to convert
object
,
Session
the Session to use for creating a JMS Message
session
,
Object
an extra object passed to the MessageConverter, e.g. the associated MethodParameter (may be null}
conversionHint
)

Implements org.springframework.jms.support.converter.SmartMessageConverter.toMessage.

A variant of toMessage(Object, Session) which takes an extra conversion context as an argument, allowing to take e.g. annotations on a payload parameter into account.

public Message

Returns:

the JMS Message
toMessage
(Object
the object to convert
object
,
Session
the Session to use for creating a JMS Message
session
,
Class<?>
the view to use to filter the content
jsonView
)

Convert a Java object to a JMS Message using the specified json view and the supplied session to create the message object.

protected Message
toMessage(Object object, Session session, ObjectWriter objectWriter)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

beanClassLoaderback to summary
private ClassLoader beanClassLoader
Annotations
@Nullable
classIdMappingsback to summary
private final Map<Class<?>, String> classIdMappings
DEFAULT_ENCODINGback to summary
public static final String DEFAULT_ENCODING

The default encoding used for writing to text messages: UTF-8.

encodingback to summary
private String encoding
Annotations
@Nullable
encodingPropertyNameback to summary
private String encodingPropertyName
Annotations
@Nullable
idClassMappingsback to summary
private Map<String, Class<?>> idClassMappings
objectMapperback to summary
private ObjectMapper objectMapper
targetTypeback to summary
private MessageType targetType
typeIdPropertyNameback to summary
private String typeIdPropertyName
Annotations
@Nullable

Constructor Detail

MappingJackson2MessageConverterback to summary
public MappingJackson2MessageConverter()

Construct a MappingJackson2MessageConverter with a default ObjectMapper.

Annotations
@SuppressWarnings:deprecation
MappingJackson2MessageConverterback to summary
public MappingJackson2MessageConverter(ObjectMapper objectMapper)

Construct a MappingJackson2MessageConverter with a custom ObjectMapper.

Parameters
objectMapper:ObjectMapper

the ObjectMapper to use

Since
6.1

Method Detail

convertFromBytesMessageback to summary
protected Object convertFromBytesMessage(BytesMessage message, JavaType targetJavaType) throws JMSException, IOException

Convert a BytesMessage to a Java Object with the specified type.

Parameters
message:BytesMessage

the input message

targetJavaType:JavaType

the target type

Returns:Object

the message converted to an object

Exceptions
JMSException:
if thrown by JMS
IOException:
in case of I/O errors
convertFromMessageback to summary
protected Object convertFromMessage(Message message, JavaType targetJavaType) throws JMSException, IOException

Template method that allows for custom message mapping. Invoked when setTargetType is not MessageType#TEXT or MessageType#BYTES.

The default implementation throws an IllegalArgumentException.

Parameters
message:Message

the input message

targetJavaType:JavaType

the target type

Returns:Object

the message converted to an object

Exceptions
JMSException:
if thrown by JMS
IOException:
in case of I/O errors
convertFromTextMessageback to summary
protected Object convertFromTextMessage(TextMessage message, JavaType targetJavaType) throws JMSException, IOException

Convert a TextMessage to a Java Object with the specified type.

Parameters
message:TextMessage

the input message

targetJavaType:JavaType

the target type

Returns:Object

the message converted to an object

Exceptions
JMSException:
if thrown by JMS
IOException:
in case of I/O errors
convertToObjectback to summary
private Object convertToObject(Message message, JavaType targetJavaType) throws JMSException, IOException

Convenience method to dispatch to converters for individual message types.

extractViewClassback to summary
private Class<?> extractViewClass(JsonView annotation, Object conversionHint)
fromMessageback to summary
public Object fromMessage(Message message) throws JMSException, MessageConversionException

Implements org.springframework.jms.support.converter.MessageConverter.fromMessage.

Doc from org.springframework.jms.support.converter.MessageConverter.fromMessage.

Convert from a JMS Message to a Java object.

Parameters
message:Message

the message to convert

Returns:Object

the converted Java object

Annotations
@Override
Exceptions
JMSException:
if thrown by JMS API methods
MessageConversionException:
in case of conversion failure
getJavaTypeForMessageback to summary
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.

Parameters
message:Message

the JMS Message from which to get the type id property

Exceptions
JMSException:
if thrown by JMS methods
See Also
setTypeIdOnMessage(Object, jakarta.jms.Message), setTypeIdPropertyName(String), setTypeIdMappings(java.util.Map)
getSerializationViewback to summary
protected Class<?> getSerializationView(Object conversionHint)

Determine a Jackson serialization view based on the given conversion hint.

Parameters
conversionHint:Object

the conversion hint Object as passed into the converter for the current conversion attempt

Returns:Class<?>

the serialization view class, or null if none

Annotations
@Nullable
mapToBytesMessageback to summary
protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, IOException

Map the given object to a BytesMessage.

Parameters
object:Object

the object to be mapped

session:Session

current JMS session

objectWriter:ObjectWriter

the writer to use

Returns:BytesMessage

the resulting message

Exceptions
JMSException:
if thrown by JMS methods
IOException:
in case of I/O errors
Since
4.3
See Also
Session#createBytesMessage
mapToMessageback to summary
protected Message mapToMessage(Object object, Session session, ObjectWriter objectWriter, MessageType targetType) throws JMSException, IOException

Template method that allows for custom message mapping. Invoked when setTargetType is not MessageType#TEXT or MessageType#BYTES.

The default implementation throws an IllegalArgumentException.

Parameters
object:Object

the object to marshal

session:Session

the JMS Session

objectWriter:ObjectWriter

the writer to use

targetType:MessageType

the target message type (other than TEXT or BYTES)

Returns:Message

the resulting message

Exceptions
JMSException:
if thrown by JMS methods
IOException:
in case of I/O errors
mapToTextMessageback to summary
protected TextMessage mapToTextMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, IOException

Map the given object to a TextMessage.

Parameters
object:Object

the object to be mapped

session:Session

current JMS session

objectWriter:ObjectWriter

the writer to use

Returns:TextMessage

the resulting message

Exceptions
JMSException:
if thrown by JMS methods
IOException:
in case of I/O errors
Since
4.3
See Also
Session#createBytesMessage
setBeanClassLoaderback to summary
public void setBeanClassLoader(ClassLoader classLoader)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Doc from org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean 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.

Parameters
classLoader:ClassLoader

the owning class loader

Annotations
@Override
setEncodingback to summary
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.

See Also
setEncodingPropertyName
setEncodingPropertyNameback to summary
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.

See Also
setEncoding
setObjectMapperback to summary
public void setObjectMapper(ObjectMapper objectMapper)

Set the ObjectMapper for this converter.

If not set, a default ObjectMapper is used.

setTargetTypeback to summary
public void setTargetType(MessageType targetType)

Specify whether 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.

See Also
MessageType#BYTES, MessageType#TEXT
setTypeIdMappingsback to summary
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.

Parameters
typeIdMappings:Map<String, Class<?>>

a Map with type id values as keys and Java classes as values

setTypeIdOnMessageback to summary
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.

Parameters
object:Object

the payload object to set a type id for

message:Message

the JMS Message on which to set the type id property

Exceptions
JMSException:
if thrown by JMS methods
See Also
getJavaTypeForMessage(jakarta.jms.Message), setTypeIdPropertyName(String), setTypeIdMappings(java.util.Map)
setTypeIdPropertyNameback to summary
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.

See Also
setTypeIdMappings
toMessageback to summary
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException

Implements org.springframework.jms.support.converter.MessageConverter.toMessage.

Doc from org.springframework.jms.support.converter.MessageConverter.toMessage.

Convert a Java object to a JMS Message using the supplied session to create the message object.

Parameters
object:Object

the object to convert

session:Session

the Session to use for creating a JMS Message

Returns:Message

the JMS Message

Annotations
@Override
Exceptions
JMSException:
if thrown by JMS API methods
MessageConversionException:
in case of conversion failure
toMessageback to summary
public Message toMessage(Object object, Session session, Object conversionHint) throws JMSException, MessageConversionException

Implements org.springframework.jms.support.converter.SmartMessageConverter.toMessage.

Doc from org.springframework.jms.support.converter.SmartMessageConverter.toMessage.

A variant of toMessage(Object, Session) which takes an extra conversion context as an argument, allowing to take e.g. annotations on a payload parameter into account.

Parameters
object:Object

the object to convert

session:Session

the Session to use for creating a JMS Message

conversionHint:Object

an extra object passed to the MessageConverter, e.g. the associated MethodParameter (may be null}

Returns:Message

the JMS Message

Annotations
@Override
Exceptions
JMSException:
if thrown by JMS API methods
MessageConversionException:
in case of conversion failure
toMessageback to summary
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.

Parameters
object:Object

the object to convert

session:Session

the Session to use for creating a JMS Message

jsonView:Class<?>

the view to use to filter the content

Returns:Message

the JMS Message

Exceptions
JMSException:
if thrown by JMS API methods
MessageConversionException:
in case of conversion failure
Since
4.3
toMessageback to summary
protected Message toMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, MessageConversionException