Class ObjectToJsonTransformer

All Implemented Interfaces:
Aware, BeanFactoryAware, BeanNameAware, InitializingBean, ApplicationContextAware, ExpressionCapable, GenericTransformer<Message<?>,Message<?>>, NamedComponent, Transformer

public class ObjectToJsonTransformer extends AbstractTransformer
Transformer implementation that converts a payload instance into a JSON string representation. By default this transformer uses JsonObjectMapperProvider factory to get an instance of a Jackson or Jackson 2 JSON-processor JsonObjectMapper implementation depending on the jackson-databind or jackson-mapper-asl libs on the classpath. Any other JsonObjectMapper implementation can be provided.

Since version 3.0, adds headers to represent the object types that were mapped from (including one level of container and Map content types). These headers are compatible with the Spring AMQP Json type mapper such that messages mapped or converted by either technology are compatible. One difference, however, is the Spring AMQP converter, when converting to JSON, sets the header types to the class name. This transformer sets the header types to the class itself.

The compatibility is achieved because, when mapping the Spring Integration message in the outbound endpoint (via the DefaultAmqpHeaderMapper), the classes are converted to String at that time.

Note: the first element of container/map types are used to determine the container/map content types. If the first element is null, the type is set to Object.

Mark Fisher, James Carr, Oleg Zhurakousky, Gary Russell, Artem Bilan
  • Field Details

  • Constructor Details

  • Method Details

    • setContentType

      public void setContentType(String contentType)
      Set the content-type header value.
      contentType - The content type.
    • getComponentType

      public String getComponentType()
      Description copied from class: IntegrationObjectSupport
      Subclasses may implement this method to provide component type information.
      Specified by:
      getComponentType in interface NamedComponent
      getComponentType in class IntegrationObjectSupport
    • doTransform

      protected Object doTransform(Message<?> message)
      Description copied from class: AbstractTransformer
      Subclasses must implement this method to provide the transformation logic. If the return value is itself a Message, it will be used as the result. Otherwise, any non-null return value will be used as the payload of the result Message.
      Specified by:
      doTransform in class AbstractTransformer
      message - The message.
      The result of the transformation.