Spring Framework

org.springframework.jms.support.converter
Class MappingJacksonMessageConverter

java.lang.Object
  extended by org.springframework.jms.support.converter.MappingJacksonMessageConverter
All Implemented Interfaces:
MessageConverter

public class MappingJacksonMessageConverter
extends java.lang.Object
implements MessageConverter

Message converter that uses the Jackson library 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.

Since:
3.1
Author:
Mark Pollack, Dave Syer, Juergen Hoeller

Field Summary
static java.lang.String DEFAULT_ENCODING
          The default encoding used for writing to text messages: UTF-8.
 
Constructor Summary
MappingJacksonMessageConverter()
           
 
Method Summary
protected  java.lang.Object convertFromBytesMessage(javax.jms.BytesMessage message, org.codehaus.jackson.type.JavaType targetJavaType)
          Convert a BytesMessage to a Java Object with the specified type.
protected  java.lang.Object convertFromMessage(javax.jms.Message message, org.codehaus.jackson.type.JavaType targetJavaType)
          Template method that allows for custom message mapping.
protected  java.lang.Object convertFromTextMessage(javax.jms.TextMessage message, org.codehaus.jackson.type.JavaType targetJavaType)
          Convert a TextMessage to a Java Object with the specified type.
 java.lang.Object fromMessage(javax.jms.Message message)
          Convert from a JMS Message to a Java object.
protected  org.codehaus.jackson.type.JavaType getJavaTypeForMessage(javax.jms.Message message)
          Determine a Jackson JavaType for the given JMS Message, typically parsing a type id message property.
protected  javax.jms.BytesMessage mapToBytesMessage(java.lang.Object object, javax.jms.Session session, org.codehaus.jackson.map.ObjectMapper objectMapper)
          Map the given object to a BytesMessage.
protected  javax.jms.Message mapToMessage(java.lang.Object object, javax.jms.Session session, org.codehaus.jackson.map.ObjectMapper objectMapper, MessageType targetType)
          Template method that allows for custom message mapping.
protected  javax.jms.TextMessage mapToTextMessage(java.lang.Object object, javax.jms.Session session, org.codehaus.jackson.map.ObjectMapper objectMapper)
          Map the given object to a TextMessage.
 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(org.codehaus.jackson.map.ObjectMapper objectMapper)
          Specify the ObjectMapper to use instead of using the default.
 void setTargetType(MessageType targetType)
          Specify whether toMessage(Object, Session) should marshal to a BytesMessage or a TextMessage.
 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, javax.jms.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.
 javax.jms.Message toMessage(java.lang.Object object, javax.jms.Session session)
          Convert a Java object to a JMS Message using the supplied session to create the message object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_ENCODING

public static final java.lang.String DEFAULT_ENCODING
The default encoding used for writing to text messages: UTF-8.

See Also:
Constant Field Values
Constructor Detail

MappingJacksonMessageConverter

public MappingJacksonMessageConverter()
Method Detail

setObjectMapper

public void setObjectMapper(org.codehaus.jackson.map.ObjectMapper objectMapper)
Specify the ObjectMapper to use instead of using the default.


setTargetType

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

setEncoding

public void setEncoding(java.lang.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 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(java.lang.String)

setEncodingPropertyName

public 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.

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(java.lang.String)

setTypeIdPropertyName

public 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.

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(java.util.Map>)

setTypeIdMappings

public void setTypeIdMappings(java.util.Map<java.lang.String,java.lang.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 - a Map with type id values as keys and Java classes as values

toMessage

public javax.jms.Message toMessage(java.lang.Object object,
                                   javax.jms.Session session)
                            throws javax.jms.JMSException,
                                   MessageConversionException
Description copied from interface: MessageConverter
Convert a Java object to a JMS Message using the supplied session to create the message object.

Specified by:
toMessage in interface MessageConverter
Parameters:
object - the object to convert
session - the Session to use for creating a JMS Message
Returns:
the JMS Message
Throws:
javax.jms.JMSException - if thrown by JMS API methods
MessageConversionException - in case of conversion failure

fromMessage

public java.lang.Object fromMessage(javax.jms.Message message)
                             throws javax.jms.JMSException,
                                    MessageConversionException
Description copied from interface: MessageConverter
Convert from a JMS Message to a Java object.

Specified by:
fromMessage in interface MessageConverter
Parameters:
message - the message to convert
Returns:
the converted Java object
Throws:
javax.jms.JMSException - if thrown by JMS API methods
MessageConversionException - in case of conversion failure

mapToTextMessage

protected javax.jms.TextMessage mapToTextMessage(java.lang.Object object,
                                                 javax.jms.Session session,
                                                 org.codehaus.jackson.map.ObjectMapper objectMapper)
                                          throws javax.jms.JMSException,
                                                 java.io.IOException
Map the given object to a TextMessage.

Parameters:
object - the object to be mapped
session - current JMS session
objectMapper - the mapper to use
Returns:
the resulting message
Throws:
javax.jms.JMSException - if thrown by JMS methods
java.io.IOException - in case of I/O errors
See Also:
Session.createBytesMessage(), Marshaller.marshal(Object, Result)

mapToBytesMessage

protected javax.jms.BytesMessage mapToBytesMessage(java.lang.Object object,
                                                   javax.jms.Session session,
                                                   org.codehaus.jackson.map.ObjectMapper objectMapper)
                                            throws javax.jms.JMSException,
                                                   java.io.IOException
Map the given object to a BytesMessage.

Parameters:
object - the object to be mapped
session - current JMS session
objectMapper - the mapper to use
Returns:
the resulting message
Throws:
javax.jms.JMSException - if thrown by JMS methods
java.io.IOException - in case of I/O errors
See Also:
Session.createBytesMessage(), Marshaller.marshal(Object, Result)

mapToMessage

protected javax.jms.Message mapToMessage(java.lang.Object object,
                                         javax.jms.Session session,
                                         org.codehaus.jackson.map.ObjectMapper objectMapper,
                                         MessageType targetType)
                                  throws javax.jms.JMSException,
                                         java.io.IOException
Template method that allows for custom message mapping. Invoked when setTargetType(org.springframework.jms.support.converter.MessageType) is not MessageType.TEXT or MessageType.BYTES.

The default implementation throws an IllegalArgumentException.

Parameters:
object - the object to marshal
session - the JMS Session
objectMapper - the mapper to use
targetType - the target message type (other than TEXT or BYTES)
Returns:
the resulting message
Throws:
javax.jms.JMSException - if thrown by JMS methods
java.io.IOException - in case of I/O errors

setTypeIdOnMessage

protected void setTypeIdOnMessage(java.lang.Object object,
                                  javax.jms.Message message)
                           throws javax.jms.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 - the payload object to set a type id for
message - the JMS Message to set the type id on
Throws:
javax.jms.JMSException - if thrown by JMS methods
See Also:
getJavaTypeForMessage(javax.jms.Message), setTypeIdPropertyName(String), setTypeIdMappings(java.util.Map)

convertFromTextMessage

protected java.lang.Object convertFromTextMessage(javax.jms.TextMessage message,
                                                  org.codehaus.jackson.type.JavaType targetJavaType)
                                           throws javax.jms.JMSException,
                                                  java.io.IOException
Convert a TextMessage to a Java Object with the specified type.

Parameters:
message - the input message
targetJavaType - the target type
Returns:
the message converted to an object
Throws:
javax.jms.JMSException - if thrown by JMS
java.io.IOException - in case of I/O errors

convertFromBytesMessage

protected java.lang.Object convertFromBytesMessage(javax.jms.BytesMessage message,
                                                   org.codehaus.jackson.type.JavaType targetJavaType)
                                            throws javax.jms.JMSException,
                                                   java.io.IOException
Convert a BytesMessage to a Java Object with the specified type.

Parameters:
message - the input message
targetJavaType - the target type
Returns:
the message converted to an object
Throws:
javax.jms.JMSException - if thrown by JMS
java.io.IOException - in case of I/O errors

convertFromMessage

protected java.lang.Object convertFromMessage(javax.jms.Message message,
                                              org.codehaus.jackson.type.JavaType targetJavaType)
                                       throws javax.jms.JMSException,
                                              java.io.IOException
Template method that allows for custom message mapping. Invoked when setTargetType(org.springframework.jms.support.converter.MessageType) is not MessageType.TEXT or MessageType.BYTES.

The default implementation throws an IllegalArgumentException.

Parameters:
message - the input message
targetJavaType - the target type
Returns:
the message converted to an object
Throws:
javax.jms.JMSException - if thrown by JMS
java.io.IOException - in case of I/O errors

getJavaTypeForMessage

protected org.codehaus.jackson.type.JavaType getJavaTypeForMessage(javax.jms.Message message)
                                                            throws javax.jms.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 - the JMS Message to set the type id on
Throws:
javax.jms.JMSException - if thrown by JMS methods
See Also:
setTypeIdOnMessage(Object, javax.jms.Message), setTypeIdPropertyName(String), setTypeIdMappings(java.util.Map)

Spring Framework