liveBook · Manning

MappingJackson2MessageConverter

As explained in Spring in Action, Sixth Edition

MappingJackson2MessageConverter is a Spring JMS converter that serializes and deserializes message payloads as JSON using Jackson.

MappingJackson2MessageConverter converts application objects to and from JSON for Spring JMS. Declare it as a bean to use it instead of the default SimpleMessageConverter, which requires objects sent as ObjectMessages to implement Serializable. section 9.1.2

@Bean
public MappingJackson2MessageConverter messageConverter() {
  MappingJackson2MessageConverter messageConverter =
                          new MappingJackson2MessageConverter();
  messageConverter.setTypeIdPropertyName("_typeId");
  return messageConverter;
}

Set a type-ID property so a receiver can identify the class to convert an incoming message into. section 9.1.2

@Bean
public MappingJackson2MessageConverter messageConverter() {
  MappingJackson2MessageConverter messageConverter =
                          new MappingJackson2MessageConverter();
  messageConverter.setTypeIdPropertyName("_typeId");
  
  Map<String, Class<?>> typeIdMappings = new HashMap<String, Class<?>>();
  typeIdMappings.put("order", TacoOrder.class);
  messageConverter.setTypeIdMappings(typeIdMappings);
  
  return messageConverter;
}

Map a short type ID such as `order` to `TacoOrder` when sender and receiver should not depend on the same fully qualified class name. section 9.1.2

With the converter configured, `JmsTemplate.convertAndSend()` can send an order object without manually creating a JMS message. section 9.1.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Shows bean configuration, the required type-ID property, and optional type mappings; stops short of converter internals.
Examples
Uses a TacoOrder sent with convertAndSend and a converter bean mapping `order` to TacoOrder.
What you can do after
Operate/configure it to send JSON-converted JMS messages and identify their types on receipt.
How it connects
Connects JmsTemplate, MessageConverter, JSON, Serializable, and JMS message type IDs.

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.