http:带杰克逊的outbound-gateway抛出解析异常以获得不匹配的响应

时间:2015-12-08 10:19:42

标签: java json spring-integration

我遇到了问题:

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

Spring Integration Config:

<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;
    }
}

纠正来自第三方的JSON响应(可行):

{
  "status" : "1"
}

第三方的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)

如何处理此解析异常?

我尝试了几个带有错误通道的解决方案(http:outbound-gateway似乎没有一个)和错误处理程序(它更像实际的http错误,如500)但无法处理Jackson解析异常。

任何想法都欢迎,欢呼各位!

3 个答案:

答案 0 :(得分:0)

已经同意的接口是什么? 老实说,第二个响应与界面定义不一致 - 我只是让他们修复它。 (对我来说,它看起来像是他们的结果,例如&#34;状态&#34;变量不存在或某事)。 否则,您需要自己处理响应。

答案 1 :(得分:0)

错误处理(error-channel)在链上游的某个组件上进行 - 例如无论什么开始你的流程。

错误消息有效内容为MessagingException,其中包含2个属性failedMessagecause

答案 2 :(得分:0)

这是一个我从未回复过的老问题,对不起,伙计们呢!

我无法让第三方改变他们的响应体,所以最终我关闭了json解析响应(json-to-object-transformer)

我用自己的json解析器解析它,所以我可以捕获解析异常。我知道,这不是最干净的解决方案,但那是我当时唯一可以做的事情。