java.lang.IllegalArgumentException: 'json' 参数必须是 [class java.lang.String, class [B

java.lang.IllegalArgumentException: 'json' argument must be an instance of: [class java.lang.String, class [B

我正在使用 Spring Rest Template 在 Hybris1811 框架中发出第三方请求。但在得到回应后,我收到以下错误:

Could not properly initialize user sessionorg.springframework.web.util.NestedServletException: 
Request processing failed; 
nested exception is java.lang.IllegalArgumentException: 'json' argument must be an instance of: [class java.lang.String, class [B, class java.io.File, class java.net.URL, class java.io.InputStream, class java.io.Reader] , but gotten: class org.springframework.http.ResponseEntity.

当我尝试执行以下代码时:

<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);
}

<int:json-to-object-transformer> 试图将来自 <int-http:outbound-gateway> 的回复转换为您的 testSubmitPaymentResponseVO 对象时出现错误。

但事实证明,回复并不是您使用 expected-response-type="java.lang.String" 所期望的纯字符串,而是整个 ResponseEntity.

我们有这样的逻辑:

    if (httpResponse.hasBody()) {
        Object responseBody = httpResponse.getBody();
        replyBuilder = (responseBody instanceof Message<?>)
                ? messageBuilderFactory.fromMessage((Message<?>) responseBody)
                : messageBuilderFactory.withPayload(responseBody); // NOSONAR - hasBody()

    }
    else {
        replyBuilder = messageBuilderFactory.withPayload(httpResponse);
    }

所以,看起来像 url="#{configurationService.configuration.getProperty('myURL')}" REST 服务 returns 没有返回给您作为 HTTP 响应。

我们无能为力:我们不控制该服务,因此您应该向他们咨询您发送给他们的请求有什么问题。