简体   繁体   English

Apache Camel:我如何从Rest API返回消息?

[英]Apache Camel: How I get message returned from Rest API?

I have a route with an error handler: 我有一条带有错误处理程序的路由:

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

And in my error handler, I call a processor in onRedelivery 在错误处理程序中,我在onRedelivery中调用处理器

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

In the error processor, I try to get the message returned by the API but I only get the message generated by the camel. 在错误处理器中,我尝试获取API返回的消息,但仅获取骆驼生成的消息。

ErrorProcessor class: ErrorProcessor类:

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

API Response: API响应:

{"messages":{"error":[{"code":500,"message":"Token doesn't exist or is expired."}]}}

Expected Message: 预期消息:

Token doesn't exist or is expired

Returned message: 返回消息:

HTTP operation failed invoking http://myurl.com/api/rest/customer/ with statusCode: 500

Try to enable the useOriginalMessage option : 尝试启用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 treats any HTTP response code other than 200 as a failure when making http calls, so you need to setThrowExceptionOnFailure to false to instruct camel route to ignore http response code and just return whatever body it receives. Camel在进行http调用时会将200以外的任何HTTP响应代码都视为失败,因此您需要将setThrowExceptionOnFailure为false,以指示骆驼路线忽略HTTP响应代码,并只返回接收到的任何内容。

Here is the Java DSL example: 这是Java DSL示例:

getContext().getEndpoint(endpoint, HttpEndpoint.class).setThrowExceptionOnFailure(false);

Make sure the endpoint is the host:port only without any http path, it does a exact match. 确保端点仅是host:port,没有任何http路径,并且确实匹配。

Refer to: http://camel.apache.org/http.html , look for throwExceptionOnFailure 参考: http : //camel.apache.org/http.html ,查找throwExceptionOnFailure

Please note, if you set it to false, then camel won't go to exception handling route, it returns as normal, and you need to handle the error response outside the camel route. 请注意,如果将其设置为false,那么骆驼将不会进入异常处理路线,它会像往常一样返回,并且您需要在骆驼路线之外处理错误响应。 I think this way is better because you have full response from the service you are calling, and you can do your error handling based on the actual failure code/reason from the response. 我认为这种方法更好,因为您可以从正在调用的服务获得完整的响应,并且可以根据响应中的实际故障代码/原因来进行错误处理。

My error here is the type of the Exception that I used. 我的错误是我使用的异常类型。

To I get the body returned by the REST, I need to use the HttpOperationFailedException. 要获取REST返回的正文,我需要使用HttpOperationFailedException。

ErrorProcessor class ErrorProcessor类

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());

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM