Java ApacheCamel:如何从RESTAPI返回消息?

Java ApacheCamel:如何从RESTAPI返回消息?,java,apache-camel,Java,Apache Camel,我有一个带有错误处理程序的路由: <route errorHandlerRef="magentoCustomerErrorHandler" id="customers.route2"> ... <to id="_to1" uri="http4://{{magento.api.url}}customer/"/> </route> API响应: public void process(Exchange exchange) throws Exception

我有一个带有错误处理程序的路由:

<route errorHandlerRef="magentoCustomerErrorHandler" id="customers.route2">
  ...
  <to id="_to1" uri="http4://{{magento.api.url}}customer/"/>
</route>
API响应:

public void process(Exchange exchange) throws Exception {
    Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
    exchange.getIn().setHeader("FailedBecause", cause.getMessage());
}
{"messages":{"error":[{"code":500,"message":"Token doesn't exist or is expired."}]}}
Token doesn't exist or is expired
HTTP operation failed invoking http://myurl.com/api/rest/customer/ with statusCode: 500
预期消息:

public void process(Exchange exchange) throws Exception {
    Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
    exchange.getIn().setHeader("FailedBecause", cause.getMessage());
}
{"messages":{"error":[{"code":500,"message":"Token doesn't exist or is expired."}]}}
Token doesn't exist or is expired
HTTP operation failed invoking http://myurl.com/api/rest/customer/ with statusCode: 500
返回消息:

public void process(Exchange exchange) throws Exception {
    Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
    exchange.getIn().setHeader("FailedBecause", cause.getMessage());
}
{"messages":{"error":[{"code":500,"message":"Token doesn't exist or is expired."}]}}
Token doesn't exist or is expired
HTTP operation failed invoking http://myurl.com/api/rest/customer/ with statusCode: 500

尝试启用
useOriginalMessage
选项:

<bean class="org.apache.camel.builder.DeadLetterChannelBuilder" id="magentoCustomerErrorHandler">
<property name="deadLetterUri" value="activemq:magento:customers:DQL"/>
<property name="onRedelivery" ref="myErrorProcessor"/>
<property name="useOriginalMessage" value="true"/>
<property name="redeliveryPolicy" ref="myRedeliveryPolicyConfig"/>


Camel在进行HTTP调用时将200以外的任何HTTP响应代码视为失败,因此您需要
将throweExceptionOnFailure
设置为false,以指示Camel路由忽略HTTP响应代码,只返回它接收到的任何正文

以下是Java DSL示例:

getContext().getEndpoint(endpoint, HttpEndpoint.class).setThrowExceptionOnFailure(false);
确保端点是host:port-only,没有任何http路径,它会进行精确匹配

请参阅:,通过异常故障查找


请注意,如果您将其设置为false,那么camel将不会转到异常处理路由,它将正常返回,并且您需要在camel路由之外处理错误响应。我认为这种方式更好,因为您调用的服务有完整的响应,并且您可以根据响应中的实际故障代码/原因进行错误处理。

我的错误就是我使用的异常类型

为了让剩下的返回主体,我需要使用HttpOperationFailedException

错误处理器类

public void process(Exchange exchange) throws Exception {
    HttpOperationFailedException cause = (HttpOperationFailedException) exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
        exchange.getIn().setHeader("FailedBecause", cause.getMessage());
        exchange.getIn().setHeader("ResponseBody", cause.getResponseBody());

}

这是行不通的。我想得到请求的主体。对我来说,使用OriginalMessage,不要更改任何内容。谢谢,说得好。但是在这一刻,我只需要获取响应主体,并将其附加到任何响应代码的原始主体200我认为使用SetThroweExceptionOnFailure对我来说不是一个好方法。问题是,如果不将ThroweExceptionOnFailure设置为false,那么Camel只会查看HTTP响应代码,并将实际响应体包装在Camel exception中的某个位置。我发现最简单的方法是告诉Camel返回Camel接收到的任何内容,您可以将请求正文保留在某个位置,以便将响应附加到请求,或者使用.rich()而不是.to(),然后您可以实现自己的聚合器。