2015-10-05 40 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>