liveBook · Manning

MessageConverter

As explained in Spring in Action, Sixth Edition

MessageConverter is a Spring interface for converting between application objects and JMS Message objects.

A MessageConverter turns an application object into a message for sending and turns a received message back into an object. With JMS, JmsTemplate.convertAndSend() uses a converter behind the scenes, so you can send an order without creating a JMS Message yourself. JmsTemplate.receiveAndConvert() performs the reverse step. section 9.1.2 section 9.1.3

Send an order without building a Message

jms.convertAndSend("tacocloud.order.queue", order); The order is converted before it is sent. Unlike jms.send(), this call does not require a MessageCreator. section 9.1.2

The JMS MessageConverter interface has two methods: toMessage(Object, Session) and fromMessage(Message). Spring’s default SimpleMessageConverter supports several message forms, but sending a domain object as an ObjectMessage requires it to implement Serializable. A Jackson-based converter is an alternative when you want JSON instead. section 9.1.2

Table 9.3 Spring message converters for common conversion tasks (all in the org.springframework.jms.support.converter package) (view table figure)

Message converter

What it does

MappingJackson2MessageConverter

Uses the Jackson 2 JSON library to convert messages to and from JSON

MarshallingMessageConverter

Uses JAXB to convert messages to and from XML

MessagingMessageConverter

Converts a Message from the messaging abstraction to and from a Message using an underlying MessageConverter for the payload and a JmsHeaderMapper to map the JMS headers to and from standard message headers

SimpleMessageConverter

Converts a String to and from a TextMessage, byte arrays to and from a BytesMessage, a Map to and from a MapMessage, and a Serializable to and from an ObjectMessage

Compare the supplied JMS converters by the data they handle, including JSON, XML, strings, byte arrays, maps, and serializable objects. section 9.1.2

Configure JSON conversion for JMS

@Bean public MappingJackson2MessageConverter messageConverter() { MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); converter.setTypeIdPropertyName("_typeId"); return converter; } Declaring the converter as a bean replaces the default. The type-ID property helps a receiver choose the type to reconstruct; type-ID mappings can also associate a short identifier such as "order" with TacoOrder. section 9.1.2

When receiving with JMS, use receiveAndConvert() if you need only the order object. Use receive() and call the converter’s fromMessage() yourself if you also need to inspect the raw message’s properties or headers. section 9.1.3

RabbitTemplate has a corresponding conversion workflow, but its MessageConverter is in the Spring AMQP package rather than the JMS package. It can convert an order for send(), while convertAndSend() handles that step automatically; receiveAndConvert() converts a queued message on receipt. For RabbitMQ JSON messages, declare a Jackson2JsonMessageConverter bean. section 9.2.2 section 9.2.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines the JMS interface’s two methods and works through built-in converters, bean configuration, and receive paths without implementing a custom converter.
Examples
Uses order-sending services, a JSON converter bean with type IDs, and queue receivers that convert messages into orders.
What you can do after
Apply it via JmsTemplate or RabbitTemplate to send domain objects and receive converted orders.
How it connects
Connects MessageConverter to JmsTemplate, RabbitTemplate, MessageCreator, MessageProperties, and receiveAndConvert().

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.