Spring Integration 5.2.2.RELEASE-使用@IntegrationConverter进行集成流转换不适用于杰克逊数据绑定

问题描述 投票:1回答:1

我将Spring Boot版本从2.1.8.RELEASE升级到2.2.2.RELEASE(因此Spring Integration从5.1升级到5.2.2.RELEASE),并且如果我们有杰克逊转换器,则用@IntegrationConverter进行自动强制转换似乎不再起作用在合格的弹簧转换器内。

由于我对项目依赖项中声明的jackson-databind具有依赖项,因此会自动声明该转换器。我需要杰克逊,因为我必须将从HTTP请求接收的有效负载转换为Java Bean。但是因为声明了此转换器,所以使用了它而不是Spring集成GenericMessageConverter,并且它转换失败,因为它首先尝试在Bean上调用toString(),然后转换toString()对象表示形式。

我在github的主机中有一个示例:https://github.com/Kruschenstein/spring-integration-playground。这是一个简单的Spring集成流程,我们在其中运行服务器,并根据用户输入请求HTTP API。典型的应用程序使用运行telnet localhost 1234输入0255之间的数字,并且有关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}]

前两个提交是工作方法。第一次提交是有效的,因为要使用àforce GenericMessageConverter。第二个提交只是表明它可以在Spring Integration 5.0中正常工作。

这是一种解决方法:

    @Bean(name = IntegrationContextUtils.ARGUMENT_RESOLVER_MESSAGE_CONVERTER_BEAN_NAME)
    public static ConfigurableCompositeMessageConverter configurableCompositeMessageConverter(
            @Qualifier(IntegrationUtils.INTEGRATION_CONVERSION_SERVICE_BEAN_NAME) ConversionService conversionService) {
        return new ConfigurableCompositeMessageConverter(
                Collections.singleton(new GenericMessageConverter(conversionService)));
    }

我在集成流程中错误地使用类型转换吗?您是否有使用新版本投射对象的新解决方案?还是只是回归?

提前感谢!

java spring-boot jackson spring-integration spring-integration-dsl
1个回答
0
投票

因此,问题确实出在MappingJackson2MessageConverter试图将Fact实例隐瞒到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问题。

© www.soinside.com 2019 - 2024. All rights reserved.