当我尝试在 java 中使用 spring 执行 get 时,出现 HttpMessageNotReadableException。 打印的 Stacktrace 说:
嵌套异常是 org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r,\n,\t) is allowed token 之间
是跟我获取的方式有关,还是跟我回复的格式有关?
我是这样调用它的:
HttpEntity<JSONObject> entity = new HttpEntity<JSONObject>();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", "application/x-gzip");
headers.set("charset", "UTF-8");
return getConnector().exchange(url, HttpMethod.GET, entity, JSONObject.class);
getConnector() 返回一个 RestConnector
响应头包括: 内容编码:gzip 内容类型:application/json
不知道是不是和编码是gzip有关
编辑: 问题是内容编码是 gzip..最后,我使用了支持 gzip 编码的 HttpClient
最佳答案
这很可能是由您的回复引起的。 如果是常规(静态)文件或手动构造的文件,请检查非法空白。
关于java - Spring HttpMessageNotReadableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19541878/