Class EmbeddedJsonHeadersMessageMapper

java.lang.Object
org.springframework.integration.support.json.EmbeddedJsonHeadersMessageMapper
All Implemented Interfaces:
BytesMessageMapper, InboundMessageMapper<byte[]>, OutboundMessageMapper<byte[]>

public class EmbeddedJsonHeadersMessageMapper extends Object implements BytesMessageMapper
For outbound messages, uses a message-aware Jackson object mapper to render the message as JSON. For messages with byte[] payloads, if rendered as JSON, Jackson performs Base64 conversion on the bytes. If the rawBytes property is true (default), the result has the form <headersLen><headers><payloadLen><payload>; with the headers rendered in JSON and the payload unchanged.

By default, all headers are included; you can provide simple patterns to specify a subset of headers.

If neither expected format is detected, or an error occurs during conversion, the payload of the message is the original byte[].

IMPORTANT

The default object mapper will only deserialize classes in certain packages.

        "java.util",
        "java.lang",
        "org.springframework.messaging.support",
        "org.springframework.integration.support",
        "org.springframework.integration.message",
        "org.springframework.integration.store"
 

To add more packages, create an object mapper using JacksonJsonUtils.messagingAwareMapper(String...).

A constructor is provided allowing the provision of such a configured object mapper.

Since:
5.0
Author:
Gary Russell, Artem Bilan
  • Field Details

    • logger

      protected final Log logger
  • Constructor Details

    • EmbeddedJsonHeadersMessageMapper

      public EmbeddedJsonHeadersMessageMapper()
      Construct an instance that embeds all headers, using the default JSON Object mapper.
    • EmbeddedJsonHeadersMessageMapper

      public EmbeddedJsonHeadersMessageMapper(String... headerPatterns)
      Construct an instance that embeds headers matching the supplied patterns, using the default JSON object mapper.
      Parameters:
      headerPatterns - the patterns.
      See Also:
    • EmbeddedJsonHeadersMessageMapper

      public EmbeddedJsonHeadersMessageMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      Construct an instance that embeds all headers, using the supplied JSON object mapper.
      Parameters:
      objectMapper - the object mapper.
    • EmbeddedJsonHeadersMessageMapper

      public EmbeddedJsonHeadersMessageMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper, String... headerPatterns)
      Construct an instance that embeds headers matching the supplied patterns using the supplied JSON object mapper.
      Parameters:
      objectMapper - the object mapper.
      headerPatterns - the patterns.
  • Method Details

    • setRawBytes

      public void setRawBytes(boolean rawBytes)
      For messages with byte[] payloads, if rendered as JSON, Jackson performs Base64 conversion on the bytes. If this property is true (default), the result has the form <headersLen><headers><payloadLen><payload>; with the headers rendered in JSON and the payload unchanged. Set to false to render the bytes as base64.
      Parameters:
      rawBytes - false to encode as base64.
    • setCaseSensitive

      public void setCaseSensitive(boolean caseSensitive)
      Set to true to make the header name pattern match case sensitive. Default false.
      Parameters:
      caseSensitive - true to make case sensitive.
    • getHeaderPatterns

      public Collection<String> getHeaderPatterns()
    • fromMessage

      public byte[] fromMessage(Message<?> message)
      Specified by:
      fromMessage in interface OutboundMessageMapper<byte[]>
    • toMessage

      public Message<?> toMessage(byte[] bytes, @Nullable Map<String,Object> headers)
      Description copied from interface: InboundMessageMapper
      Convert a provided object to the Message and supply with headers if necessary and provided.
      Specified by:
      toMessage in interface BytesMessageMapper
      Specified by:
      toMessage in interface InboundMessageMapper<byte[]>
      Parameters:
      bytes - the object for message payload or some other conversion logic
      headers - additional headers for building message. Can be null
      Returns:
      the message as a result of mapping