![](/img/trans.png)
[英]java.lang.IllegalArgumentException: Bad class: class java.lang.String
[英]java.lang.IllegalArgumentException: 'json' argument must be an instance of: [class java.lang.String, class [B
我正在使用 Spring Rest 模板在 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: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:json-to-object-transformer> 嘗試將來自<int-http:outbound-gateway>
的回復轉換為您的testSubmitPaymentResponseVO
object 時,錯誤來自<int:json-to-object-transformer>
。
但事實證明,reply 不是您期望使用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 服務不會作為 HTTP 響應返回給您。
我們無能為力:我們無法控制該服務,因此您應該向他們咨詢您發送給他們的請求中有什么問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.