gpt4 book ai didi

java - Camel REST 服务在返回 null 时抛出异常

转载 作者:行者123 更新时间:2023-12-01 13:56:05 25 4
gpt4 key购买 nike

我有一个简单的 REST 服务。有客户,我可以通过其 ID 获得客户。如果没有客户端具有请求的 ID,则应返回 404 not found。

相关部分如下:

rest("/client")
.consumes("application/json").produces("application/json")
.get("{id}")
.to("direct:getClient");

from("direct:getClient")
.bean(clientService, "getClient(${header.id})")
.choice()
.when(simple("${body} == null"))
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(404));

当可以找到客户端时一切正常,但是,当找不到客户端并且 clientService 返回 null 时,我得到以下堆栈跟踪:

org.apache.camel.RuntimeCamelException: java.io.IOException: Stream closed
at org.apache.camel.http.common.HttpMessage.createBody(HttpMessage.java:74)
at org.apache.camel.impl.MessageSupport.getBody(MessageSupport.java:47)
at org.apache.camel.processor.CamelInternalProcessor$StreamCachingAdvice.after(CamelInternalProcessor.java:799)
at org.apache.camel.processor.CamelInternalProcessor$StreamCachingAdvice.after(CamelInternalProcessor.java:767)
at org.apache.camel.processor.CamelInternalProcessor$InternalCallback.done(CamelInternalProcessor.java:246)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:573)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:120)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:83)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197)
at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:62)
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:145)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:542)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:197)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:120)

我想不通这个。从 bean 方法调用返回 null 应该是可能的,对吧?

最佳答案

尝试作为变通方法使用

.setBody().method(clientService, "getClient(${header.id})")

我怀疑它的 .bean() 在您返回一个 null 值作为新消息正文时有一个小错误,这会愚弄底层 HttpMessage 以为消息正文尚未初始化,因此再次读取流时会出现流错误。

关于java - Camel REST 服务在返回 null 时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40293128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com