我需要在拦截器中从 ClientHttpResponse 接收到 response.statusCode(),应该在我创建的 testSubmitPaymentResponseVO 对象中可用,以便可以相应地进行错误处理。但是我不知道在哪里以及如何将响应传递给我的 POJO testSubmitPaymentResponseVO。
<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"/>
拦截器代码:
public class TestRequestLoggingInterceptor implements ClientHttpRequestInterceptor
{
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException
{
ClientHttpResponse response;
try
{
request.getHeaders().setAcceptCharset(Collections.singletonList(Charsets.UTF_8));
long startTimeinMillis = new Date().getTime();
response = execution.execute(request, body);
logResponse(response);
}
catch (Exception e)
{
LOGGER.error("Error when trying to fetch the information : " + e.getMessage());
throw new IOException("Connection was unsuccessful!", e);
}
return response;
}
private void logResponse(ClientHttpResponse response) throws IOException
{
String lineSeparator = System.lineSeparator();
String responseBody = StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8"));
StringBuilder responseLog = new StringBuilder();
responseLog.append("=======================Response Begin==========================").append(lineSeparator);
responseLog.append("Status code : {" + response.getStatusCode() + "}").append(lineSeparator);
responseLog.append("Status text : {" + response.getStatusText() + "}").append(lineSeparator);
responseLog.append("Headers : {" + response.getHeaders() + "}").append(lineSeparator);
responseLog.append("Response body: {" + responseBody + "}").append(lineSeparator);
responseLog.append("======================Response End==============================").append(lineSeparator);
}