发送请求后如何处理错误(camel-http)?

问题描述 投票:0回答:1

我想根据http代码响应处理错误。

我也想知道如何在我的路线上启用*throwExceptionOnFailure*。例如,如果响应code is 500x,则将消息发送到队列“redmine_errors”

更新4:

从答案@ fg78nc添加异常后我的蓝图(不工作)

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
        http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
        http://cxf.apache.org/blueprint/jaxws http://cxf.apache.org/schemas/blueprint/jaxws.xsd
        http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
       ">

<bean id="tfsToRedmineMapper"
    class="com.stackabuse.example.TfsToRedmineMapper" />

<bean id="myBean" class="com.stackabuse.example.MyBean" />

<camelContext
    xmlns="http://camel.apache.org/schema/blueprint">

<onException>
    <exception>org.apache.camel.http.common.HttpOperationFailedException
    </exception>
    <onWhen>
        <method ref="myBean" method="parseException" />
    </onWhen>
    <handled>
        <constant>true</constant>
    </handled>
    <to uri="log:redmine_errors" />
</onException>
    <route>
        <from uri="jetty:http://0.0.0.0:8082/test" />
        <inOnly uri="activemq://from_tfs" />
    </route>
    <route>
        <from uri="activemq://from_tfs" />
        <process ref="tfsToRedmineMapper" />
        <to uri="activemq://for_redmine" />
    </route>
    <route>
        <from uri="activemq://for_redmine" />
        <setHeader headerName="Content-Type">
            <constant>application/json; charset=utf-8</constant>
        </setHeader>
        <setHeader headerName="X-Redmine-API-Key">
            <constant>my_redmine_api_token</constant>
        </setHeader>
        <toD uri="${header.url}" />
    </route>

错误:2019-02-15 09:35:12,103 | ERROR | mix-7.0.1/deploy | BlueprintCamelContext | 40 - org.apache.camel.camel-blueprint - 2.16.5 | Error occurred during starting Camel: CamelContext(camel-32) due Failed to create route route48 at: >>> OnException[null When[bean{} -> []] -> [To[activemq://redmine_errors]]] <<< in route: Route(route48)[[From[jetty:http://0.0.0.0:8082/test]] -> [On... because of org.apache.camel.http.common.HttpOperationFailedException org.apache.camel.FailedToCreateRouteException: Failed to create route route48 at: >>> OnException[null When[bean{} -> []] -> [To[activemq://redmine_errors]]] <<< in route: Route(route48)[[From[jetty:http://0.0.0.0:8082/test]] -> [On... because of org.apache.camel.http.common.HttpOperationFailedException

enter image description here

enter image description here

java apache-camel apache-servicemix blueprint-osgi camel-http
1个回答
1
投票

不幸的是,Camel没有正确设置Http状态代码。下面的解决方案有点复杂,但它的工作原理。它也可以使用简单的语言谓词在XML中解决,但不知何故它对我不起作用,所以我使用Java作为谓词。

蓝图:

 <bean id="myBean" class="com.example.MyBean" />

 <onException>
     <exception>org.apache.camel.http.common.HttpOperationFailedException</exception>
      <onWhen>
         <method ref="myBean" method="parseException" />
      </onWhen>
      <handled>
         <constant>true</constant>
      </handled>
      <to uri="jms:redmine_errors"/>
 </onException>

Java:

       package com.example;

       public class MyBean {

       public boolean parseException(Exchange exchange){
              return exchange.getProperty("CamelExceptionCaught")
                             .toString().contains("statusCode: 500");
            }
       }
© www.soinside.com 2019 - 2024. All rights reserved.