2015-10-05 97 views
1

如何处理HTTP出站网关中的异常? 当我收到状态代码500或400 ...时,显示一个例外。那么我应该怎么做来处理使用spring集成的http错误。弹簧集成:处理HTTP错误与oubound网关

我的配置是这样的:

<int:inbound-channel-adapter channel="quakeinfotrigger.channel" 
    expression="''"> 
    <int:poller fixed-delay="60000"></int:poller> 
</int:inbound-channel-adapter> 


<int:channel id="quakeinfo.channel"> 
    <int:queue capacity="10" /> 
</int:channel> 

<int:channel id="quakeinfotrigger.channel"></int:channel> 

<int:channel id="error.channel"> 
<int:queue capacity="10" /> 
</int:channel> 

<int:service-activator input-channel="error.channel" 
    ref="httpResponseErrorHandler" method="handleMessage"> 
    <int:poller fixed-delay="5000"></int:poller> 
</int:service-activator> 

<int:service-activator input-channel="quakeinfo.channel" 
    ref="httpResponseMessageHandler" method="handleMessage"> 
    <int:poller fixed-delay="5000"></int:poller> 
</int:service-activator> 

<int:gateway id="requestGateway" service-interface="standalone.HttpRequestGateway" 
    default-request-channel="quakeinfotrigger.channel" error-channel="error.channel" /> 

<int-http:outbound-gateway id="quakerHttpGateway" 
    request-channel="quakeinfotrigger.channel" url="http://fooo/mmmm/rest/put/44545454" 
    http-method="PUT" expected-response-type="java.lang.String" charset="UTF-8" 
    reply-timeout="5000" reply-channel="quakeinfo.channel"> 
</int-http:outbound-gateway> 

<bean id="httpResponseMessageHandler" class="standalone.HttpResponseMessageHandler" /> 
<bean id="httpResponseErrorHandler" class="standalone.HttpResponseErrorHandler" /> 

我想知道为什么例外一点儿也不去回复道

回答

0

我想知道为什么例外情况nt去回复通道

因为处理很自然例外情况,呃例外情况。

Spring集成中有(至少)两种方式来处理异常。

  1. 添加一个error-channel和任何启动流程的相关流程(例如网关)。错误通道获得MessagingException有效载荷的ErrorMessage;该例外有两个属性 - failedMessagecause
  2. 向网关添加ExpressionEvaluatingRequestHandlerAdvice(或自定义建议);见Adding Behavior to Endpoints
+0

错误通道已经加入到网关,但总是一个HttpServerErrorException被抛出,我的配置文件被更新 – NAZEHA

+0

因为你没有使用网关;入站适配器直接发送到'quakeinfotrigger.channel'。您也可以在入站适配器的轮询器中添加一个“错误通道”;那么你根本就不需要网关。 –

+0

谢谢你加里。有用 !!! – NAZEHA

0

如果响应状态代码是在HTTP系列CLIENT_ERRORSERVER_ERRORHttpClientErrorExceptionHttpServerErrorException分别抛出。因此,响应不走回复道

参考DefaultResponseErrorHandler
方法:hasError和的HandleError

来处理这些异常,创建自己的CustomResponseErrorHandler和覆盖的hasError和handlerError方法的内容。

public class CustomResponseErrorHandler extends DefaultResponseErrorHandler { 

    @Override 
    public boolean hasError(ClientHttpResponse response) throws IOException { 
     return hasError(getHttpStatusCode(response)); 
    } 

    protected boolean hasError(HttpStatus statusCode) { 
     return /*Status Code to be considered as Error*/ ; 
    } 

    @Override 
    public void handleError(ClientHttpResponse response) throws IOException { /* Handle Exceptions */ 
    } 
} 

并添加错误处理您HTTP的出站网关

<int-http:outbound-gateway id="quakerHttpGateway" 
    request-channel="quakeinfotrigger.channel" url="http://fooo/mmmm/rest/put/44545454" 
    http-method="PUT" expected-response-type="java.lang.String" charset="UTF-8" 
    reply-timeout="5000" reply-channel="quakeinfo.channel" error-handler="customResponseErrorHandler"> 
</int-http:outbound-gateway>