Class JacksonJsonMessageConverter
- All Implemented Interfaces:
- Aware,- BeanClassLoaderAware,- MessageConverter,- SmartMessageConverter
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.
 
The default constructor loads JacksonModules
 found by MapperBuilder.findModules(ClassLoader).
- Since:
- 7.0
- Author:
- Sebastien Deleuze
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringThe default encoding used for writing to text messages: UTF-8.
- 
Constructor SummaryConstructorsConstructorDescriptionConstruct a new instance with aJsonMappercustomized with theJacksonModules found byMapperBuilder.findModules(ClassLoader).JacksonJsonMessageConverter(ObjectMapper objectMapper) Construct a new instance with the providedObjectMapper.
- 
Method SummaryModifier and TypeMethodDescriptionprotected ObjectconvertFromBytesMessage(BytesMessage message, JavaType targetJavaType) Convert a BytesMessage to a Java Object with the specified type.protected ObjectconvertFromMessage(Message message, JavaType targetJavaType) Template method that allows for custom message mapping.protected ObjectconvertFromTextMessage(TextMessage message, JavaType targetJavaType) Convert a TextMessage to a Java Object with the specified type.fromMessage(Message message) Convert from a JMS Message to a Java object.protected JavaTypegetJavaTypeForMessage(Message message) Determine a Jackson JavaType for the given JMS Message, typically parsing a type id message property.getSerializationView(@Nullable Object conversionHint) Determine a Jackson serialization view based on the given conversion hint.protected BytesMessagemapToBytesMessage(Object object, Session session, ObjectWriter objectWriter) Map the given object to aBytesMessage.protected MessagemapToMessage(Object object, Session session, ObjectWriter objectWriter, MessageType targetType) Template method that allows for custom message mapping.protected TextMessagemapToTextMessage(Object object, Session session, ObjectWriter objectWriter) Map the given object to aTextMessage.voidsetBeanClassLoader(ClassLoader classLoader) Callback that supplies the beanclass loaderto a bean instance.voidsetEncoding(String encoding) Specify the encoding to use when converting to and from text-based message body content.voidsetEncodingPropertyName(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.voidsetTargetType(MessageType targetType) voidsetTypeIdMappings(Map<String, Class<?>> typeIdMappings) Specify mappings from type ids to Java classes, if desired.protected voidsetTypeIdOnMessage(Object object, Message message) Set a type id for the given payload object on the given JMS Message.voidsetTypeIdPropertyName(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.Convert a Java object to a JMS Message using the supplied session to create the message object.Convert a Java object to a JMS Message using the specified json view and the supplied session to create the message object.A variant ofMessageConverter.toMessage(Object, Session)which takes an extra conversion context as an argument, allowing to take, for example, annotations on a payload parameter into account.protected MessagetoMessage(Object object, Session session, ObjectWriter objectWriter) 
- 
Field Details- 
DEFAULT_ENCODINGThe default encoding used for writing to text messages: UTF-8.- See Also:
 
 
- 
- 
Constructor Details- 
JacksonJsonMessageConverterpublic JacksonJsonMessageConverter()Construct a new instance with aJsonMappercustomized with theJacksonModules found byMapperBuilder.findModules(ClassLoader).
- 
JacksonJsonMessageConverterConstruct a new instance with the providedObjectMapper.- See Also:
 
 
- 
- 
Method Details- 
setTargetTypeSpecify whethertoMessage(Object, Session)should marshal to aBytesMessageor aTextMessage.The default is MessageType.BYTES, i.e. this converter marshals to aBytesMessage. Note that the default version of this converter supportsMessageType.BYTESandMessageType.TEXTonly.- See Also:
 
- 
setEncodingSpecify 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:
 
- 
setEncodingPropertyNameSpecify 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:
 
- 
setTypeIdPropertyNameSpecify 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:
 
- 
setTypeIdMappingsSpecify 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
 
- 
setBeanClassLoaderDescription copied from interface:BeanClassLoaderAwareCallback that supplies the beanclass loaderto a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean'sInitializingBean.afterPropertiesSet()method or a custom init-method.- Specified by:
- setBeanClassLoaderin interface- BeanClassLoaderAware
- Parameters:
- classLoader- the owning class loader
 
- 
toMessagepublic Message toMessage(Object object, Session session) throws JMSException, MessageConversionException Description copied from interface:MessageConverterConvert a Java object to a JMS Message using the supplied session to create the message object.- Specified by:
- toMessagein 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
 
- 
toMessagepublic Message toMessage(Object object, Session session, @Nullable Object conversionHint) throws JMSException, MessageConversionException Description copied from interface:SmartMessageConverterA variant ofMessageConverter.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:
- toMessagein 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:
 
- 
toMessagepublic Message toMessage(Object object, Session session, @Nullable 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- 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
 
- 
fromMessageDescription copied from interface:MessageConverterConvert from a JMS Message to a Java object.- Specified by:
- fromMessagein 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
 
- 
toMessageprotected Message toMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, MessageConversionException 
- 
mapToTextMessageprotected TextMessage mapToTextMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, IOException Map the given object to aTextMessage.- 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
- See Also:
 
- 
mapToBytesMessageprotected BytesMessage mapToBytesMessage(Object object, Session session, ObjectWriter objectWriter) throws JMSException, IOException Map the given object to aBytesMessage.- 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
- See Also:
 
- 
mapToMessageprotected Message mapToMessage(Object object, Session session, ObjectWriter objectWriter, MessageType targetType) throws JMSException, IOException Template method that allows for custom message mapping. Invoked whensetTargetType(org.springframework.jms.support.converter.MessageType)is notMessageType.TEXTorMessageType.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
 
- 
setTypeIdOnMessageSet 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:
 
- 
convertFromTextMessageprotected Object convertFromTextMessage(TextMessage message, JavaType targetJavaType) throws JMSException, 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:
- JMSException- if thrown by JMS
- IOException- in case of I/O errors
 
- 
convertFromBytesMessageprotected Object convertFromBytesMessage(BytesMessage message, JavaType targetJavaType) throws JMSException, 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:
- JMSException- if thrown by JMS
- IOException- in case of I/O errors
 
- 
convertFromMessageprotected Object convertFromMessage(Message message, JavaType targetJavaType) throws JMSException, IOException Template method that allows for custom message mapping. Invoked whensetTargetType(org.springframework.jms.support.converter.MessageType)is notMessageType.TEXTorMessageType.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
 
- 
getJavaTypeForMessageDetermine 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:
 
- 
getSerializationViewDetermine 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 nullif none
 
 
-