Class MappingJackson2MessageConverter

java.lang.Object
org.springframework.jms.support.converter.MappingJackson2MessageConverter
All Implemented Interfaces:
Aware, BeanClassLoaderAware, MessageConverter, SmartMessageConverter

@Deprecated(since="7.0", forRemoval=true) public class MappingJackson2MessageConverter extends Object implements SmartMessageConverter, BeanClassLoaderAware
Deprecated, for removal: This API element is subject to removal in a future version.
since 7.0 in favor of JacksonJsonMessageConverter
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:

  • MapperFeature.DEFAULT_VIEW_INCLUSION is disabled
  • DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES is disabled
Since:
3.1.4
Author:
Mark Pollack, Dave Syer, Juergen Hoeller, Stephane Nicoll
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Deprecated, for removal: This API element is subject to removal in a future version.
    The default encoding used for writing to text messages: UTF-8.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Deprecated, for removal: This API element is subject to removal in a future version.
    Construct a MappingJackson2MessageConverter with a default ObjectMapper.
    MappingJackson2MessageConverter(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Construct a MappingJackson2MessageConverter with a custom ObjectMapper.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected Object
    convertFromBytesMessage(BytesMessage message, com.fasterxml.jackson.databind.JavaType targetJavaType)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Convert a BytesMessage to a Java Object with the specified type.
    protected Object
    convertFromMessage(Message message, com.fasterxml.jackson.databind.JavaType targetJavaType)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Template method that allows for custom message mapping.
    protected Object
    convertFromTextMessage(TextMessage message, com.fasterxml.jackson.databind.JavaType targetJavaType)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Convert a TextMessage to a Java Object with the specified type.
    Deprecated, for removal: This API element is subject to removal in a future version.
    Convert from a JMS Message to a Java object.
    protected com.fasterxml.jackson.databind.JavaType
    Deprecated, for removal: This API element is subject to removal in a future version.
    Determine a Jackson JavaType for the given JMS Message, typically parsing a type id message property.
    protected @Nullable Class<?>
    Deprecated, for removal: This API element is subject to removal in a future version.
    Determine a Jackson serialization view based on the given conversion hint.
    protected BytesMessage
    mapToBytesMessage(Object object, Session session, com.fasterxml.jackson.databind.ObjectWriter objectWriter)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Map the given object to a BytesMessage.
    protected Message
    mapToMessage(Object object, Session session, com.fasterxml.jackson.databind.ObjectWriter objectWriter, MessageType targetType)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Template method that allows for custom message mapping.
    protected TextMessage
    mapToTextMessage(Object object, Session session, com.fasterxml.jackson.databind.ObjectWriter objectWriter)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Map the given object to a TextMessage.
    void
    Deprecated, for removal: This API element is subject to removal in a future version.
    Callback that supplies the bean class loader to a bean instance.
    void
    setEncoding(String encoding)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Specify the encoding to use when converting to and from text-based message body content.
    void
    setEncodingPropertyName(String encodingPropertyName)
    Deprecated, for removal: This API element is subject to removal in a future version.
    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(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Set the ObjectMapper for this converter.
    void
    Deprecated, for removal: This API element is subject to removal in a future version.
    Specify whether toMessage(Object, Session) should marshal to a BytesMessage or a TextMessage.
    void
    setTypeIdMappings(Map<String,Class<?>> typeIdMappings)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Specify mappings from type ids to Java classes, if desired.
    protected void
    setTypeIdOnMessage(Object object, Message message)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Set a type id for the given payload object on the given JMS Message.
    void
    setTypeIdPropertyName(String typeIdPropertyName)
    Deprecated, for removal: This API element is subject to removal in a future version.
    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.
    toMessage(Object object, Session session)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Convert a Java object to a JMS Message using the supplied session to create the message object.
    protected Message
    toMessage(Object object, Session session, com.fasterxml.jackson.databind.ObjectWriter objectWriter)
    Deprecated, for removal: This API element is subject to removal in a future version.
     
    toMessage(Object object, Session session, @Nullable Class<?> jsonView)
    Deprecated, for removal: This API element is subject to removal in a future version.
    Convert a Java object to a JMS Message using the specified json view and the supplied session to create the message object.
    toMessage(Object object, Session session, @Nullable Object conversionHint)
    Deprecated, for removal: This API element is subject to removal in a future version.
    A variant of MessageConverter.toMessage(Object, Session) which takes an extra conversion context as an argument, allowing to take, for example, annotations on a payload parameter into account.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_ENCODING

      public static final String DEFAULT_ENCODING
      Deprecated, for removal: This API element is subject to removal in a future version.
      The default encoding used for writing to text messages: UTF-8.
      See Also:
  • Constructor Details

    • MappingJackson2MessageConverter

      public MappingJackson2MessageConverter()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Construct a MappingJackson2MessageConverter with a default ObjectMapper.
    • MappingJackson2MessageConverter

      public MappingJackson2MessageConverter(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Construct a MappingJackson2MessageConverter with a custom ObjectMapper.
      Parameters:
      objectMapper - the ObjectMapper to use
      Since:
      6.1
  • Method Details

    • setObjectMapper

      public void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Set the ObjectMapper for this converter.

      If not set, a default ObjectMapper is used.

    • setTargetType

      public void setTargetType(MessageType targetType)
      Deprecated, for removal: This API element is subject to removal in a future version.
      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:
    • setEncoding

      public void setEncoding(String encoding)
      Deprecated, for removal: This API element is subject to removal in a future version.
      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

      public void setEncodingPropertyName(String encodingPropertyName)
      Deprecated, for removal: This API element is subject to removal in a future version.
      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:
    • setTypeIdPropertyName

      public void setTypeIdPropertyName(String typeIdPropertyName)
      Deprecated, for removal: This API element is subject to removal in a future version.
      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

      public void setTypeIdMappings(Map<String,Class<?>> typeIdMappings)
      Deprecated, for removal: This API element is subject to removal in a future version.
      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
    • setBeanClassLoader

      public void setBeanClassLoader(ClassLoader classLoader)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Description copied from interface: BeanClassLoaderAware
      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.

      Specified by:
      setBeanClassLoader in interface BeanClassLoaderAware
      Parameters:
      classLoader - the owning class loader
    • toMessage

      public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException
      Deprecated, for removal: This API element is subject to removal in a future version.
      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:
      JMSException - if thrown by JMS API methods
      MessageConversionException - in case of conversion failure
    • toMessage

      public Message toMessage(Object object, Session session, @Nullable Object conversionHint) throws JMSException, MessageConversionException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Description copied from interface: SmartMessageConverter
      A variant of MessageConverter.toMessage(Object, Session) which takes an extra conversion context as an argument, allowing to take, for example, annotations on a payload parameter into account.
      Specified by:
      toMessage in interface SmartMessageConverter
      Parameters:
      object - the object to convert
      session - the Session to use for creating a JMS Message
      conversionHint - an extra object passed to the MessageConverter, for example, the associated MethodParameter (may be null)
      Returns:
      the JMS Message
      Throws:
      JMSException - if thrown by JMS API methods
      MessageConversionException - in case of conversion failure
      See Also:
    • toMessage

      public Message toMessage(Object object, Session session, @Nullable Class<?> jsonView) throws JMSException, MessageConversionException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Convert a Java object to a JMS Message using the specified json view and the supplied session to create the message object.
      Parameters:
      object - the object to convert
      session - the Session to use for creating a JMS Message
      jsonView - the view to use to filter the content
      Returns:
      the JMS Message
      Throws:
      JMSException - if thrown by JMS API methods
      MessageConversionException - in case of conversion failure
      Since:
      4.3
    • fromMessage

      public Object fromMessage(Message message) throws JMSException, MessageConversionException
      Deprecated, for removal: This API element is subject to removal in a future version.
      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:
      JMSException - if thrown by JMS API methods
      MessageConversionException - in case of conversion failure
    • toMessage

      protected Message toMessage(Object object, Session session, com.fasterxml.jackson.databind.ObjectWriter objectWriter) throws JMSException, MessageConversionException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Throws:
      JMSException
      MessageConversionException
    • mapToTextMessage

      protected TextMessage mapToTextMessage(Object object, Session session, com.fasterxml.jackson.databind.ObjectWriter objectWriter) throws JMSException, IOException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Map the given object to a TextMessage.
      Parameters:
      object - the object to be mapped
      session - current JMS session
      objectWriter - the writer to use
      Returns:
      the resulting message
      Throws:
      JMSException - if thrown by JMS methods
      IOException - in case of I/O errors
      Since:
      4.3
      See Also:
    • mapToBytesMessage

      protected BytesMessage mapToBytesMessage(Object object, Session session, com.fasterxml.jackson.databind.ObjectWriter objectWriter) throws JMSException, IOException
      Deprecated, for removal: This API element is subject to removal in a future version.
      Map the given object to a BytesMessage.
      Parameters:
      object - the object to be mapped
      session - current JMS session
      objectWriter - the writer to use
      Returns:
      the resulting message
      Throws:
      JMSException - if thrown by JMS methods
      IOException - in case of I/O errors
      Since:
      4.3
      See Also:
    • mapToMessage

      protected Message mapToMessage(Object object, Session session, com.fasterxml.jackson.databind.ObjectWriter objectWriter, MessageType targetType) throws JMSException, IOException
      Deprecated, for removal: This API element is subject to removal in a future version.
      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
      objectWriter - the writer to use
      targetType - the target message type (other than TEXT or BYTES)
      Returns:
      the resulting message
      Throws:
      JMSException - if thrown by JMS methods
      IOException - in case of I/O errors
    • setTypeIdOnMessage

      protected void setTypeIdOnMessage(Object object, Message message) throws JMSException
      Deprecated, for removal: This API element is subject to removal in a future version.
      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 on which to set the type id property
      Throws:
      JMSException - if thrown by JMS methods
      See Also:
    • convertFromTextMessage

      protected Object convertFromTextMessage(TextMessage message, com.fasterxml.jackson.databind.JavaType targetJavaType) throws JMSException, IOException
      Deprecated, for removal: This API element is subject to removal in a future version.
      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:
      JMSException - if thrown by JMS
      IOException - in case of I/O errors
    • convertFromBytesMessage

      protected Object convertFromBytesMessage(BytesMessage message, com.fasterxml.jackson.databind.JavaType targetJavaType) throws JMSException, IOException
      Deprecated, for removal: This API element is subject to removal in a future version.
      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:
      JMSException - if thrown by JMS
      IOException - in case of I/O errors
    • convertFromMessage

      protected Object convertFromMessage(Message message, com.fasterxml.jackson.databind.JavaType targetJavaType) throws JMSException, IOException
      Deprecated, for removal: This API element is subject to removal in a future version.
      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:
      JMSException - if thrown by JMS
      IOException - in case of I/O errors
    • getJavaTypeForMessage

      protected com.fasterxml.jackson.databind.JavaType getJavaTypeForMessage(Message message) throws JMSException
      Deprecated, for removal: This API element is subject to removal in a future version.
      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, for example, doing some heuristics based on message origin.

      Parameters:
      message - the JMS Message from which to get the type id property
      Throws:
      JMSException - if thrown by JMS methods
      See Also:
    • getSerializationView

      protected @Nullable Class<?> getSerializationView(@Nullable Object conversionHint)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Determine a Jackson serialization view based on the given conversion hint.
      Parameters:
      conversionHint - the conversion hint Object as passed into the converter for the current conversion attempt
      Returns:
      the serialization view class, or null if none