Class JsonToObjectTransformer
java.lang.Object
org.springframework.integration.context.IntegrationObjectSupport
org.springframework.integration.transformer.AbstractTransformer
org.springframework.integration.json.JsonToObjectTransformer
- All Implemented Interfaces:
Aware
,BeanClassLoaderAware
,BeanFactoryAware
,BeanNameAware
,InitializingBean
,ApplicationContextAware
,ExpressionCapable
,NamedComponent
,GenericTransformer<Message<?>,
,Message<?>> Transformer
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:
-
Field Summary
Fields inherited from class org.springframework.integration.context.IntegrationObjectSupport
EXPRESSION_PARSER, logger
-
Constructor Summary
ConstructorDescriptionJsonToObjectTransformer
(Class<?> targetClass) JsonToObjectTransformer
(Class<?> targetClass, JsonObjectMapper<?, ?> jsonObjectMapper) JsonToObjectTransformer
(ResolvableType targetType) Construct an instance based on the providedResolvableType
.JsonToObjectTransformer
(ResolvableType targetType, JsonObjectMapper<?, ?> jsonObjectMapper) Construct an instance based on the providedResolvableType
andJsonObjectMapper
.JsonToObjectTransformer
(JsonObjectMapper<?, ?> jsonObjectMapper) -
Method Summary
Modifier and TypeMethodDescriptionprotected Object
doTransform
(Message<?> message) Subclasses must implement this method to provide the transformation logic.Subclasses may implement this method to provide component type information.protected void
onInit()
Subclasses may implement this for initialization logic.void
setBeanClassLoader
(ClassLoader classLoader) void
setValueTypeExpression
(Expression valueTypeExpression) Configure a SpELExpression
to evaluate aResolvableType
to instantiate the payload from the incoming JSON.void
setValueTypeExpressionString
(String valueTypeExpressionString) Configure a SpEL expression to evaluate aResolvableType
to instantiate the payload from the incoming JSON.Methods inherited from class org.springframework.integration.transformer.AbstractTransformer
transform
Methods inherited from class org.springframework.integration.context.IntegrationObjectSupport
afterPropertiesSet, extractTypeIfPossible, generateId, getApplicationContext, getApplicationContextId, getBeanDescription, getBeanFactory, getBeanName, getChannelResolver, getComponentName, getConversionService, getExpression, getIntegrationProperties, getIntegrationProperty, getMessageBuilderFactory, getTaskScheduler, isInitialized, setApplicationContext, setBeanFactory, setBeanName, setChannelResolver, setComponentName, setConversionService, setMessageBuilderFactory, setPrimaryExpression, setTaskScheduler, toString
-
Constructor Details
-
JsonToObjectTransformer
public JsonToObjectTransformer() -
JsonToObjectTransformer
-
JsonToObjectTransformer
Construct an instance based on the providedResolvableType
.- Parameters:
targetType
- theResolvableType
to use.- Since:
- 5.2
-
JsonToObjectTransformer
-
JsonToObjectTransformer
public JsonToObjectTransformer(@Nullable Class<?> targetClass, @Nullable JsonObjectMapper<?, ?> jsonObjectMapper) -
JsonToObjectTransformer
public JsonToObjectTransformer(ResolvableType targetType, @Nullable JsonObjectMapper<?, ?> jsonObjectMapper) Construct an instance based on the providedResolvableType
andJsonObjectMapper
.- Parameters:
targetType
- theResolvableType
to use.jsonObjectMapper
- theJsonObjectMapper
to use.- Since:
- 5.2
-
-
Method Details
-
setBeanClassLoader
- Specified by:
setBeanClassLoader
in interfaceBeanClassLoaderAware
-
setValueTypeExpressionString
Configure a SpEL expression to evaluate aResolvableType
to instantiate the payload from the incoming JSON. By default this transformer consultsJsonHeaders
in the request message. If this expression returnsnull
orResolvableType
building throws aClassNotFoundException
, this transformer falls back to the providedtargetType
. This logic is present as an expression becauseJsonHeaders
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
Configure a SpELExpression
to evaluate aResolvableType
to instantiate the payload from the incoming JSON. By default this transformer consultsJsonHeaders
in the request message. If this expression returnsnull
orResolvableType
building throws aClassNotFoundException
, this transformer falls back to the providedtargetType
. This logic is present as an expression becauseJsonHeaders
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 SpELExpression
to use.- Since:
- 5.2.6
-
getComponentType
Description copied from class:IntegrationObjectSupport
Subclasses may implement this method to provide component type information.- Specified by:
getComponentType
in interfaceNamedComponent
- Overrides:
getComponentType
in classIntegrationObjectSupport
-
onInit
protected void onInit()Description copied from class:IntegrationObjectSupport
Subclasses may implement this for initialization logic.- Overrides:
onInit
in classIntegrationObjectSupport
-
doTransform
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 classAbstractTransformer
- Parameters:
message
- The message.- Returns:
- The result of the transformation.
-