Class JsonToObjectTransformer

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

public class JsonToObjectTransformer extends AbstractTransformer implements BeanClassLoaderAware
Transformer implementation that converts a JSON string payload into an instance of the provided target Class. By default this transformer uses JsonObjectMapperProvider factory to get an instance of Jackson JSON-processor if jackson-databind lib is present on the classpath. Any other JsonObjectMapper implementation can be provided.

Since version 3.0, you can omit the target class and the target type can be determined by the JsonHeaders type entries - including the contents of a one-level container or map type.

The type headers can be classes or fully-qualified class names.

Since version 5.2.6, a SpEL expression option is provided to let to build a target ResolvableType somehow externally.

Since:
2.0
Author:
Mark Fisher, Artem Bilan
See Also:
  • Constructor Details

    • JsonToObjectTransformer

      public JsonToObjectTransformer()
    • JsonToObjectTransformer

      public JsonToObjectTransformer(@Nullable Class<?> targetClass)
    • JsonToObjectTransformer

      public JsonToObjectTransformer(ResolvableType targetType)
      Construct an instance based on the provided ResolvableType.
      Parameters:
      targetType - the ResolvableType to use.
      Since:
      5.2
    • JsonToObjectTransformer

      public JsonToObjectTransformer(@Nullable JsonObjectMapper<?,?> jsonObjectMapper)
    • JsonToObjectTransformer

      public JsonToObjectTransformer(@Nullable Class<?> targetClass, @Nullable JsonObjectMapper<?,?> jsonObjectMapper)
    • JsonToObjectTransformer

      public JsonToObjectTransformer(ResolvableType targetType, @Nullable JsonObjectMapper<?,?> jsonObjectMapper)
      Construct an instance based on the provided ResolvableType and JsonObjectMapper.
      Parameters:
      targetType - the ResolvableType to use.
      jsonObjectMapper - the JsonObjectMapper to use.
      Since:
      5.2
  • Method Details

    • setBeanClassLoader

      public void setBeanClassLoader(ClassLoader classLoader)
      Specified by:
      setBeanClassLoader in interface BeanClassLoaderAware
    • setValueTypeExpressionString

      public void setValueTypeExpressionString(String valueTypeExpressionString)
      Configure a SpEL expression to evaluate a ResolvableType to instantiate the payload from the incoming JSON. By default this transformer consults JsonHeaders in the request message. If this expression returns null or ResolvableType building throws a ClassNotFoundException, this transformer falls back to the provided targetType. This logic is present as an expression because JsonHeaders may not have real class values, but rather some type ids which have to be mapped to target classes according some external registry.
      Parameters:
      valueTypeExpressionString - the SpEL expression to use.
      Since:
      5.2.6
    • setValueTypeExpression

      public void setValueTypeExpression(Expression valueTypeExpression)
      Configure a SpEL Expression to evaluate a ResolvableType to instantiate the payload from the incoming JSON. By default this transformer consults JsonHeaders in the request message. If this expression returns null or ResolvableType building throws a ClassNotFoundException, this transformer falls back to the provided targetType. This logic is present as an expression because JsonHeaders may not have real class values, but rather some type ids which have to be mapped to target classes according some external registry.
      Parameters:
      valueTypeExpression - the SpEL Expression to use.
      Since:
      5.2.6
    • 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
      Overrides:
      getComponentType in class IntegrationObjectSupport
    • onInit

      protected void onInit()
      Description copied from class: IntegrationObjectSupport
      Subclasses may implement this for initialization logic.
      Overrides:
      onInit 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
      Parameters:
      message - The message.
      Returns:
      The result of the transformation.