【发布时间】:2021-08-18 19:57:07
【问题描述】:
我正在使用 HttpRequestExecutingMessageHandler 调用外部服务器。我正在使用 JSON 到对象转换器来转换 JSON 数据。但我收到以下异常。
原因:com.fasterxml.jackson.core.JsonParseException:非法字符((CTRL-CHAR,代码31)):标记之间只允许常规空格(\r,\n,\t) 在 [Source: (String)"�
当我检查标题时,我发现了以下内容。 Transfer-Encoding=chunked
这是异常日志的原因吗?
出站网关和JsonToObjectTransformer如下:
@ServiceActivator(inputChannel = "channelOutboundRequest")
@Bean
public HttpRequestExecutingMessageHandler outboundGateway() {
final HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler(
endpoint);
handler.setExpectedResponseType(String.class);
handler.setHttpMethod(HttpMethod.POST);
handler.setOutputChannelName("channelResponse");
handler.setAdviceChain(Collections.singletonList(advice()));
return handler;
}
@Bean
@Transformer(inputChannel = "channelResponse", outputChannel = "channelReply")
public JsonToObjectTransformer transformer(ObjectMapper objectMapper) {
final JsonObjectMapper<?, ?> mapper = new Jackson2JsonObjectMapper(objectMapper);
return new JsonToObjectTransformer(DetailsDTO.class, mapper);
}
如果标题导致问题,我该如何处理响应?
注意:如果我直接使用邮递员访问外部服务器,我会得到 JSON 结构的响应。
我不知道这里出了什么问题。如果我使用如下简单的 restemplate 调用,它可以正常工作。
JSONObject jsonObject = new JSONObject("{\"code\":\"F001\",\"transactionId\":\"1008566223232\"}");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setBearerAuth("token");
HttpEntity<String> request =
new HttpEntity<String>(jsonObject.toString(), headers);
String respns = restTemplt.postForObject("http://endpoint", request, String.class);
System.out.println(respns);
JSONObject response = new JSONObject(respns);
我能找到的一个不同之处在于响应标头。如下:
出站网关响应标头:- {Transfer-Encoding=chunked、http_requestMethod=GET、errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@d8b195e、Server=nginx、Accept=/、连接=keep-alive, User-Agent=PostmanRuntime/7.28.0, Host=localhost:8901, Accept-Encoding=gzip, deflate, br, http_statusCode=200 OK, Date=1622533072000, Authorization=Bearer令牌,replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@d8b195e,Cache-Control=no-cache,ETag=W/"1009-5SzdL+uWyY6ZcMWht5dMtm2Sxlc",Content-Encoding=gzip,http_requestUrl=http:// inboundurl, id=be07fc8d-d478-5fa9-33e4-61a2b5f92468, Content-Length=207, contentType=application/json;charset=utf-8, Content-Type=application/json, requestFrom=CUSTOM_HEADER, 时间戳=1622533092827}
正常的 restTemplate 调用响应头 [服务器:“nginx”,日期:“星期二,2021 年 6 月 1 日 07:34:54 GMT”,内容类型:“application/json; charset=utf-8”,内容长度:“4105”,连接:“ keep-alive",访问控制允许来源:"*",内容安全策略:"default-src 'self';base-uri 'self';block-all-mixed-content;font-src ' self' https: 数据:;frame-ancestors 'self';img-src 'self' 数据:;object-src 'none';script-src 'self';script-src-attr 'none';style-src' self' https: 'unsafe-inline';upgrade-insecure-requests", X-DNS-Prefetch-Control:"off", Expect-CT:"max-age=0", X-Frame-Options:"SAMEORIGIN" , Strict-Transport-Security:"max-age=15552000; includeSubDomains", X-Download-Options:"noopen", X-Content-Type-Options:"nosniff", X-Permitted-Cross-Domain-Policies:" none", Referrer-Policy:"no-referrer", X-XSS-Protection:"0", ETag:"W/"1009-llD9DqxYkEsjyikWajYk+16cb1k""]
有人可以帮忙吗?
【问题讨论】:
标签: spring spring-boot spring-integration spring-integration-http