Java Spring集成5.2.2.RELEASE-集成流转换与@IntegrationConverter不';我不能和jackson数据绑定一起工作
我将我的Spring Boot版本从Java Spring集成5.2.2.RELEASE-集成流转换与@IntegrationConverter不';我不能和jackson数据绑定一起工作,java,spring-boot,jackson,spring-integration,spring-integration-dsl,Java,Spring Boot,Jackson,Spring Integration,Spring Integration Dsl,我将我的Spring Boot版本从2.1.8.RELEASE升级到2.2.2.RELEASE(因此Spring Integration从5.1升级到5.2.2.RELEASE),如果我们在符合条件的Spring转换器中有一个jackson转换器,则带有@IntegrationConverter的自动转换似乎不再起作用 转换器是自动声明的,因为我在项目依赖项中声明了对jackson-databind的依赖项。我需要jackson,因为我必须将从HTTP请求接收的有效负载转换为JavaBean。但
2.1.8.RELEASE
升级到2.2.2.RELEASE
(因此Spring Integration从5.1
升级到5.2.2.RELEASE
),如果我们在符合条件的Spring转换器中有一个jackson转换器,则带有@IntegrationConverter
的自动转换似乎不再起作用
转换器是自动声明的,因为我在项目依赖项中声明了对jackson-databind
的依赖项。我需要jackson,因为我必须将从HTTP请求接收的有效负载转换为JavaBean。但是由于声明了此转换器,因此使用它来代替Spring integrationGenericMessageConverter
,它无法转换,因为它首先尝试在bean上调用toString()
,然后转换toString()
对象表示
我在github的主机中有一个示例:。这是一个简单的Spring集成流,我们在其中运行服务器,根据用户输入请求HTTP API。典型的应用程序使用是运行一个telnet localhost 1234
输入一个介于0
和255
之间的数字,有关cat的事实将显示在用户终端中
这是我从telnet
客户端发送请求时得到的错误日志:
org.springframework.integration.transformer.MessageTransformationException: Failed to transform Message in bean 'server.transformer#2' for component 'server.org.springframework.integration.config.ConsumerEndpointFactoryBean#5'; defined in: 'org.grorg.integration.IntegrationApplication'; from source: 'bean method server'; nested exception is org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Unrecognized token 'org': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: (String)"org.grorg.integration.model.api.Fact@22fae1e0"; line: 1, column: 4]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'org': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: (String)"org.grorg.integration.model.api.Fact@22fae1e0"; line: 1, column: 4], failedMessage=GenericMessage [payload=org.grorg.integration.model.api.Fact@22fae1e0, headers={errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@cdb23ed, Server=Cowboy, ip_tcp_remotePort=54966, NUM=1, Connection=keep-alive, ip_localInetAddress=/127.0.0.1, ip_address=127.0.0.1, http_statusCode=200 OK, Date=1578329608000, Via=1.1 vegur, replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@cdb23ed, Etag=W/"10db9-+ezvkUu4LsJ+zW4+jWiIrVy9v5E", ip_connectionId=localhost:54966:1234:2a75cd25-8e85-4b49-a124-4db58aac3b84, Set-Cookie=connect.sid=s%3A8-aFqwlU8601G1vSPXCnc4Wskla8GzpS.JBIKqFX1%2Fw%2BU7py1QOKqI2G1%2FdJNeUPBxGdGtbXQGIw; Path=/; HttpOnly, id=83b6790f-27f0-6045-3e0e-8b852ca10b46, Content-Length=69049, contentType=application/json;charset=utf-8, ip_hostname=localhost, timestamp=1578329608525}]
前两个提交是工作方法。第一次提交之所以有效,是因为要使用的是一个强制GenericMessageConverter
。第二次提交只是表明它在SpringIntegration5.0中正常工作
这是一个解决方法:
@Bean(name=IntegrationContextUtils.ARGUMENT\u解析器\u消息\u转换器\u Bean\u name)
公共静态可配置CompositeMessageConverter可配置CompositeMessageConverter(
@限定符(IntegrationUtils.INTEGRATION\u CONVERSION\u SERVICE\u BEAN\u NAME)ConversionService ConversionService){
返回新的可配置CompositeMessageConverter(
singleton(新的GenericMessageConverter(conversionService));
}
我是否错误地使用集成流的类型转换?您有新版本的铸造对象的新解决方案吗?还是这只是一种倒退
提前谢谢 因此,问题确实在于
映射Jackson2MessageConverter
试图将事实
实例转换为CatFact
。这一次发生在Http.outboundGateway()
之后-响应带有contentType
头作为application/json
。这确实是一个提示,MappingJackson2MessageConverter
尝试将一个对象转换为另一个对象
当我在您的流程中执行此操作时:
.transform(Message.class, m -> {
Facts facts = (Facts) m.getPayload();
int num = (int) m.getHeaders().get("NUM");
return facts.getAll().get(num);
})
.headerFilter(MessageHeaders.CONTENT_TYPE)
.transform(CatFact.class, id -> id)
(请注意headerFilter()
),它将通过GenericMessageConverter
返回工作状态
这不是错误或回归。这只是报头和负载之间状态不一致的事实。我们可能需要考虑如何减轻它,虽然。。。请随意提出GH问题并加以思考。我正在抽取您在本地玩的示例。敬请期待。感谢像Jackson这样的书抱怨,因为它被要求解析字符串org.grorg.integration.model.api。Fact@22fae1e0“作为JSON。。。但事实并非如此。调查一下。。。