Java 在Spring集成中处理http响应

Java 在Spring集成中处理http响应,java,spring,spring-integration,Java,Spring,Spring Integration,我有一个http:outbound网关,如下所示: <int:channel id="fromEmployeeChannel" /> <int-http:outbound-gateway url="localhost:8080/api/employees" request-channel="fromEmployeeChannel" http-method="POST" expected-response-type="java.lang.Str

我有一个http:outbound网关,如下所示:

 <int:channel id="fromEmployeeChannel" />
 <int-http:outbound-gateway
        url="localhost:8080/api/employees" request-channel="fromEmployeeChannel"
        http-method="POST" expected-response-type="java.lang.String" reply-channel="employeeAdded" >
 </int-http:outbound-gateway>


如何处理不同的HTTP响应状态?另外,如果收到404 Not Found之类的错误,如何处理该错误?

HttpRequestExecutingMessageHandler将
响应状态填充到
replyMessage
标题中:

replyBuilder.setHeader(org.springframework.integration.http.HttpHeaders.STATUS_CODE, httpResponse.getStatusCode());
因此,您可以在
下游执行
,以针对不同逻辑向不同通道发送响应

404
和类似文件分别由
RestTemplate
中的
DefaultResponseErrorHandler
(默认文件)处理,并作为异常从那里抛出:
HttpClientErrorException
HttpServerErrorException
。甚至像
RestClientException(“未知状态代码[“+statusCode+”])”

首先,此异常被抛出到上游,例如轮询或消息驱动端点上的
错误通道

但从另一方面来说,您可以在
上对它们进行逻辑处理。为此,它具有
,并且内置了
表达式评估RequestHandlerAdvice
,能够对FailureExpression执行操作: