java.io.IOException:使用Camel REST DSL时流关闭,并且存在通过onException

时间:2018-11-21 12:39:40

标签: apache-camel

我有这个Camel rest端点,它以json格式接收销售线索:

<rests id="restLeads" xmlns="http://camel.apache.org/schema/spring">
    <rest path="/leads">
        <post id="createLead" uri="/" consumes="application/json" >
            <to uri="direct:leads"/>
        </post>
    </rest>
</rests>

处理管道中的下一条路线是:

    onException(PredicateValidationException.class)
            .handled(true)
            .setHeader(Exchange.HTTP_RESPONSE_CODE).simple("400").to("log:validationError");

    from("direct:leads")
            .id("leadEnricher")
            .unmarshal().json(Lead.class, null)
            .validate(it -> {
                final Lead lead = it.getIn().getBody(Lead.class);
                return lead.isValid();
            })
            ...

此处未对线索进行整理和验证。如果它通过验证,则一切正常。但是,当发生验证错误时,会触发onException块,并且我可以看到正确的log:validationError消息:

validationError  INFO - Exchange[ExchangePattern: InOut, BodyType: com.codependent.dto.Lead, Body: Lead(email=null, product=HOME, pricing=20.0)]

此外,主叫客户端会收到预期的400错误代码:HTTP/1.1 400 Bad Request

尽管它似乎可以正常工作,但在日志末尾出现此错误:

o.a.c.c.s.CamelHttpTransportServlet ERROR - Error processing request
java.io.IOException: Stream closed
    at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:372) ~[tomcat-embed-core-8.0.36.redhat-30.jar:8.0.36]
    at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:156) ~[tomcat-embed-core-8.0.36.redhat-30.jar:8.0.36]
    at org.apache.camel.util.IOHelper.copy(IOHelper.java:202) ~[camel-core-2.21.0.fuse-000112-redhat-3.jar:2.21.0.fuse-000112-redhat-3]
    at org.apache.camel.http.common.DefaultHttpBinding.copyStream(DefaultHttpBinding.java:432) ~[camel-http-common-2.21.0.fuse-000112-redhat-3.jar:2.21.0.fuse-000112-redhat-3]
    at org.apache.camel.http.common.DefaultHttpBinding.doWriteDirectResponse(DefaultHttpBinding.java:496) ~[camel-http-common-2.21.0.fuse-000112-redhat-3.jar:2.21.0.fuse-000112-redhat-3]
    at org.apache.camel.http.common.DefaultHttpBinding.doWriteResponse(DefaultHttpBinding.java:395) ~[camel-http-common-2.21.0.fuse-000112-redhat-3.jar:2.21.0.fuse-000112-redhat-3]
    at org.apache.camel.http.common.DefaultHttpBinding.writeResponse(DefaultHttpBinding.java:322) ~[camel-http-common-2.21.0.fuse-000112-redhat-3.jar:2.21.0.fuse-000112-redhat-3]

为什么会出现流关闭错误?

更新:

奇怪的是,如果我明确设置主体,该错误不再显示,为什么?

onException(PredicateValidationException.class)
            .handled(true)
            .setBody().simple("Validation error for: ${body}")
            .setHeader(Exchange.HTTP_RESPONSE_CODE).simple("400").to("log:validationError");

0 个答案:

没有答案