Java 能否在Spring集成的其他类中提供HTTPS响应状态代码
我需要做出响应。在拦截器中从ClientHttpResponse接收的statusCode()应该在我创建的testSubmitPaymentResponseVO对象中可用,以便可以相应地进行错误处理。但是,我不知道在哪里以及如何将响应传递给我的POJO testSubmitPaymentResponseVOJava 能否在Spring集成的其他类中提供HTTPS响应状态代码,java,spring,spring-integration,resttemplate,spring-resttemplate,Java,Spring,Spring Integration,Resttemplate,Spring Resttemplate,我需要做出响应。在拦截器中从ClientHttpResponse接收的statusCode()应该在我创建的testSubmitPaymentResponseVO对象中可用,以便可以相应地进行错误处理。但是,我不知道在哪里以及如何将响应传递给我的POJO testSubmitPaymentResponseVO <int:channel id="MytestReqRequestChannel"/> <int:header-enricher input-cha
<int:channel id="MytestReqRequestChannel"/>
<int:header-enricher input-channel="MytestReqRequestChannel" output-channel="MytestReqEnrichedRequestChannel">
<int:header name="x-api-key" value="#{configurationService.configuration.getProperty('myKey')}"/>
<int:header name="Content-Type" value="application/json;charset=UTF-8" />
</int:header-enricher>
<int:object-to-json-transformer input-channel="MytestReqEnrichedRequestChannel"
output-channel="MytestReqJSONRequestChannel"/>
<int-http:outbound-gateway
url="#{configurationService.configuration.getProperty('myURL')}"
http-method="POST"
header-mapper="testPaymentHeaderMapper"
rest-template="testSubmitPaymentRestTemplate"
request-channel="MytestReqJSONRequestChannel"
reply-channel="MytestReqJSONResponseChannel"
charset="UTF-8"
expected-response-type="java.lang.String">
</int-http:outbound-gateway>
<int:json-to-object-transformer input-channel="MytestReqJSONResponseChannel"
output-channel="MytestReqResponseChannel"
type="com.test.testb2bintegrations.models.payment.request.testSubmitPaymentResponseVO"/>
<int:channel id="MytestReqResponseChannel"/>
不清楚你的意思是什么;如果状态代码不是200,则通常不会有JSON字符串转换为POJO
您可以向网关添加一个
expressionEvaluationRequestHandlerAdvice
,处理异常,并在那里创建一个适当的JSON字符串。请参见,我在评论中为您提供了一些说明,以便回答您的问题:
我们可能会认为这是一种重复,但如果我在这里重复一遍,那也不会伤害到我。
当
处理ClientHttpResponse
时,HTTP响应状态代码存储在相应的报头中:
else {
replyBuilder = messageBuilderFactory.withPayload(httpResponse);
}
replyBuilder.setHeader(org.springframework.integration.http.HttpHeaders.STATUS_CODE,
httpResponse.getStatusCode());
return replyBuilder.copyHeaders(headers);
这个标题是简单的下游供您考虑
如果您确实希望在testSubmitPaymentResponseVO
中显示该状态代码,那么您可能需要在其上设置一个setter
其中一种方法是实现一些transformer方法来接受您的testSubmitPaymentResponseVO
和该标头,并将其设置到POJO中,然后从该标头返回:
testSubmitPaymentResponseVO setStatusCode(testSubmitPaymentResponseVO pojo, @Header(name = HttpHeaders.STATUS_CODE) HttpStatus statusCode) {
pojo.setStatusCode(statusCode);
return pojo;
}
另一种方法是使用
组件:
<int:enricher id="userEnricher"
input-channel="setStatusCodeChannel"
output-channel="MytestReqResponseChannel">
<int:property name="StatusCode" expression="headers[http_statusCode]"/>
</int:enricher>
您的
应该输出到这个新的setStatusCodeChannel
参见文档:是的。。。一切皆有可能。看我的答案:看起来它满足了要求。。。
<int:enricher id="userEnricher"
input-channel="setStatusCodeChannel"
output-channel="MytestReqResponseChannel">
<int:property name="StatusCode" expression="headers[http_statusCode]"/>
</int:enricher>