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

查看:69
本文介绍了java.lang.IllegalArgumentException:'json'参数必须是以下实例:[class java.lang.String,class [B的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Rest Template在Hybris1811框架中提出第三方请求.但是获得响应后,我得到以下错误提示:

I am making a third party request in Hybris1811 framework using Spring Rest Template . But after getting response I am Getting below error:

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.

当我尝试实现以下代码时:

when I try to implement the below code:

< int:网关ID ="testRequestGateway";服务接口="com.test.testb2bintegrations.payment.gateway.testRequestGateway".default-request-channel ="MytestReqRequestChannel";default-reply-channel ="MytestReqResponseChannel"/>

<int:gateway id="testRequestGateway" service-interface="com.test.testb2bintegrations.payment.gateway.testRequestGateway" default-request-channel="MytestReqRequestChannel" default-reply-channel="MytestReqResponseChannel"/>

<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-http:outbound-gateway> 放入您的 testSubmitPaymentResponseVO 对象.

The error comes from the <int:json-to-object-transformer> when it tries to convert a reply from the <int-http:outbound-gateway> into your testSubmitPaymentResponseVO object.

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

But it turns out that reply is not a plain string as you would expect using expected-response-type="java.lang.String", but the whole 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服务没有任何内容作为HTTP响应返回给您.

So, looks like that url="#{configurationService.configuration.getProperty('myURL')}" REST service returns nothing back to you as an HTTP response.

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

Nothing what we can help with: we don't control that service so you should consult with them what is wrong in the request you send to them.

这篇关于java.lang.IllegalArgumentException:'json'参数必须是以下实例:[class java.lang.String,class [B的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆