我在使用REST API时遇到问题。下面是我的休息客户端。它在服务调用时失败,错误:
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value; nested exception is com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string value
at [Source: java.io.PushbackInputStream@62a8dd06; line: 11, column: 55]
当我在postman中使用相同的json字符串(打印在日志中)时,它可以工作。当我从客户端触发请求时,它失败了。
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", "Basic " + base64Creds);
try {
request = mapper.constructRequest(txnRequest, params);
logger.debug(method + " Request: " + request);
ObjectMapper map = new ObjectMapper();
logger.debug(method + " Request in json format: " +
map.writeValueAsString(request));
myRequest = new HttpEntity<MyRequest>(request, headers);
response = restTemplate.exchange(url, HttpMethod.POST, myRequest, MyResponse.class);
logger.debug(method + " response: " + response);
} catch(Exception ex) {
ex.printStackTrace();
}
任何帮助都非常感谢。
我切换到传统的HttpURLConnection来完成这个工作。通过这种方式,我可以用utf-8设置输入和输出格式,并且它工作了。我试图用rest模板做同样的事情,但问题仍然存在。因此,我要将客户端实现切换为HttpURLConnection