我正在努力配置Spring Integration DSL转换器使用的“自定义”ObjectMapper。
我收到了一个java.time.Instant
json表示,我想解析它们到对象属性。即:
{“type”:“TEST”,“source”:“TEST”,“timestamp”:{“epochSecond”:1454503381,“nano”:335000000}}
该消息是一条kafka消息,它提出了一个问题:我是否应该编写一个实现Kafka编码器/解码器的自定义序列化器,以便能够将kafka消息转换为正确的对象,或者spring-integration必须自动执行此操作? / p>
fw / dependencies和version:
Spring Boot - 1.3.2.RELEASE
Spring Integration Java Dsl - 1.1.1.RELEASE
FasterXml Jackson - 2.6.5
我已根据Jackson文档将此Java配置添加到项目中: https://github.com/FasterXML/jackson-datatype-jsr310
@Configuration
public class IntegrationConfiguration {
@Bean
public JsonObjectMapper<JsonNode, JsonParser> jsonObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
return new Jackson2JsonObjectMapper(mapper);
}
}
以及以下Jackson JSR-310人工制品:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.6.5</version>
</dependency>
基于Spring博客上的这篇文章,我甚至不必注册新的Java8时间模块。 https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring#jackson-modules
这是我得到的例外:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class java.time.Instant]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
at [Source: {"type":"TEST","source":"TEST","timestamp":{"epochSecond":1454503381,"nano":335000000}}; line: 1, column: 71] (through reference chain: my.app.MyDto["timestamp"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1106)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:296)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:133)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:258)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3736)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2764)
at org.springframework.integration.support.json.Jackson2JsonObjectMapper.fromJson(Jackson2JsonObjectMapper.java:75)
at org.springframework.integration.support.json.Jackson2JsonObjectMapper.fromJson(Jackson2JsonObjectMapper.java:44)
at org.springframework.integration.support.json.AbstractJacksonJsonObjectMapper.fromJson(AbstractJacksonJsonObjectMapper.java:56)
at org.springframework.integration.json.JsonToObjectTransformer.doTransform(JsonToObjectTransformer.java:78)
at org.springframework.integration.transformer.AbstractTransformer.transform(AbstractTransformer.java:33)
... 74 more
解决:
问题是我期望Spring会检测到jackson-datatype-jsr310原型并注册JavaTimeModule,但它并不完全正常。
我们有两种方法可以解决这个问题:
1.如果我们按原样使用Spring Boot和Spring Integration,则接受的答案
2.如果使用Spring Integration Dsl,只需将IntegrationConfiguration类与jsonObjectMapper()bean保持一致并使用它:
@Autowired
private JsonObjectMapper jsonObjectMapper;
return IntegrationFlows
.from(inboundChannel())
.transform(Transformers.fromJson(myDto.class, jsonObjectMapper))
...
答案 0 :(得分:2)
Spring Boot上没有任何关系来强制Spring Integration使用它。
您只需要使用JsonToObjectTransformer
:
jsonObjetMapper()
@Bean
@Transformer(inputChannel="input", outputChannel="output")
JsonToObjectTransformer jsonToObjectTransformer() {
return new JsonToObjectTransformer(jsonObjectMapper());
}
虽然没有理由将JsonObjectMapper
注册为bean。
答案 1 :(得分:0)
您是否已为频道适配器定义了编码器?
对于您正在使用的适配器,入站通道适配器或消息驱动器通道适配器,您应始终使用编码器。
在你的情况下,StringEncoder应该解决问题。
<bean id="myEncoder" class="org.springframework.integration.kafka.serializer.common.StringEncoder"/>