java - http :outbound-gateway with Jackson throws parsing exception for unmatching Response

标签 java json spring-integration

我有一个问题:

如何处理第 3 方以 http:outbound-gateway 未知的格式返回 json 响应的情况(以及其中的 jackson - com.fasterxml.jackson 之一)。

<小时/>

Spring 集成配置:

<int:chain input-channel="ul_campaignSetup_channel">
    <int:header-enricher>
        <int:header name="Content-Type" value="application/json; charset=UTF-8"/>
    </int:header-enricher>

    <int-http:outbound-gateway id="ulHttpOutboundGateway"
                               http-method="POST"
                               url="${request.url}"
                               extract-request-payload="true"
                               error-handler="campaignSetupResponseHandler"
                               expected-response-type="integration.ul.message.CampaignSetupResponse"
                               request-factory="ulRequestFactory">
        <int-http:uri-variable name="url" expression="headers.url"/>
    </int-http:outbound-gateway>
    <int:service-activator method="handle" ref="campaignSetupResponseHandler" />

    <int:json-to-object-transformer id="myId" />

</int:chain>

<bean id="campaignSetupResponseHandler" class="integration.ul.message.CampaignSetupResponseHandler" />

<bean id="jackson2http" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="objectMapper" ref="ulObjectMapper" />
</bean>

<bean id="ulObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper"/>

<bean id="ulRequestFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory">
    <property name="connectTimeout" value="10000" />
    <property name="readTimeout" value="10000" />
</bean>
<小时/>
public class CampaignSetupResponse {

    private String status;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}
<小时/>

来自第 3 方的正确 JSON 响应(有效):

{
  "status" : "1"
}
<小时/>

来自第 3 方的错误 JSON 响应(导致异常):

{
  "nonExistingProperty": "non existing value"
}
<小时/>

我得到的异常:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "nonExistingProperty" (class integration.ul.message.CampaignSetupResponse), not marked as ignorable (one known property: "status"])
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@20fddd96; line: 2, column: 27] (through reference chain: integration.ul.message.CampaignSetupResponse["nonExistingProperty"]); nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "nonExistingProperty" (class integration.ul.message.CampaignSetupResponse), not marked as ignorable (one known property: "status"])
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@20fddd96; line: 2, column: 27] (through reference chain: integration.ul.message.CampaignSetupResponse["nonExistingProperty"])
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:228)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:220)
<小时/>

如何处理这个解析异常?

我尝试了一些带有错误 channel (http:outbound-gateway 似乎没有)和错误处理程序(更多的是针对实际的 http 错误,如 500)的解决方案,但无法处理 Jackson 解析异常。

欢迎任何想法,干杯!

最佳答案

已经商定的接口(interface)是什么? 老实说,第二个响应与接口(interface)定义不相符 - 我只是让他们修复它。 (对我来说,它看起来像是他们的末端的东西,比如“状态”变量不存在或其他东西)。 否则您需要自己处理响应。

关于java - http :outbound-gateway with Jackson throws parsing exception for unmatching Response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34153329/

相关文章:

java - 与 WebSphere 和 Spring 集成进行 SSH 配置

java - 如何修复从 start.spring.io 初始化的新 Spring Boot 应用程序的 Maven 构建中的错误

java - java mail api 邮件发送成功与否

Java 分割分隔符

java - MOXy 兼容 XML 和 JSON 响应

javascript - 无法识别传单上的颜色

java - 两者均实现了JavaLaunchHelper类。将使用两者之一。哪一个未定义

java - 来自 Spring MVC 中 LocalDateTime(java 8) 的 Json 字符串

error-handling - Spring集成errorChannel在JUnit中没有消息

spring - 部署 spring 应用程序时出现 javax.management.InstanceAlreadyExistsException