JSON 无效的 UTF-8 中间字节
当(Jackson,本例中)JSON 引擎尝试解析某些未以 UTF-8 编码的 JSON 时,会发生此错误。
如何告诉引擎它应该期待与 UTF-8 不同的内容,例如 UTF-16?
HttpHeaders requestHeaders = createSomeHeader();
RestTemplate restTemplate = new RestTemplate();
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
String url = "someurl"
ResponseEntity<MyObject[]> arrayResponseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, MyObject[].class);
错误日志:
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Invalid UTF-8 middle byte 0x20
at [Source: org.apache.http.conn.EofSensorInputStream@44d397b0; line: 92, column: 42]; nested exception is org.codehaus.jackson.JsonParseException: Invalid UTF-8 middle byte 0x20
at [Source: org.apache.http.conn.EofSensorInputStream@44d397b0; line: 92, column: 42]
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:138)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:74)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:622)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:608)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:449)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:404)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:380)
... 4 more
Caused by: org.codehaus.jackson.JsonParseException: Invalid UTF-8 middle byte 0x20
at [Source: org.apache.http.conn.EofSensorInputStream@44d397b0; line: 92, column: 42]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1213)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:375)
at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidOther(Utf8StreamParser.java:2132)
at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidOther(Utf8StreamParser.java:2139)
at org.codehaus.jackson.impl.Utf8StreamParser._decodeUtf8_3fast(Utf8StreamParser.java:1962)
This error happens when the (Jackson, this case) JSON engine tries to parse some JSON that is not encoded in UTF-8.
How to tell the engine that it should expect something different from UTF-8, such as UTF-16?
HttpHeaders requestHeaders = createSomeHeader();
RestTemplate restTemplate = new RestTemplate();
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
String url = "someurl"
ResponseEntity<MyObject[]> arrayResponseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, MyObject[].class);
error log:
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Invalid UTF-8 middle byte 0x20
at [Source: org.apache.http.conn.EofSensorInputStream@44d397b0; line: 92, column: 42]; nested exception is org.codehaus.jackson.JsonParseException: Invalid UTF-8 middle byte 0x20
at [Source: org.apache.http.conn.EofSensorInputStream@44d397b0; line: 92, column: 42]
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:138)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:74)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:622)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:608)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:449)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:404)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:380)
... 4 more
Caused by: org.codehaus.jackson.JsonParseException: Invalid UTF-8 middle byte 0x20
at [Source: org.apache.http.conn.EofSensorInputStream@44d397b0; line: 92, column: 42]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1213)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:375)
at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidOther(Utf8StreamParser.java:2132)
at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidOther(Utf8StreamParser.java:2139)
at org.codehaus.jackson.impl.Utf8StreamParser._decodeUtf8_3fast(Utf8StreamParser.java:1962)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当我在 Java 客户端应用程序中序列化这样的 JSON
并且在服务器端使用 Spring Boot 作为 REST 端点时,我遇到了此异常。
异常是:
嵌套异常是 com.fasterxml.jackson.databind.JsonMappingException:无效的 UTF-8 起始字节 0xaa
我的问题是,我没有在 HTTP 客户端上设置正确的编码。
这解决了我的问题:
Android 设置内容类型 HttpPost
I got this exception when in the Java Client Application I was serializing a JSON like this
and on the Server Side I was using Spring Boot as REST Endpoint.
Exception was:
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 start byte 0xaa
My problem was, that I was not setting the correct encoding on the HTTP Client.
This solved my problem:
Android set content type HttpPost
JSON 数据必须编码为 UTF-8、UTF-16 或 UTF-32。 JSON 解码器可以通过检查前四个八位字节来确定编码字节流:
听起来服务器正在以某种非法编码(ISO-8859-1、windows-1252 等)对数据进行编码
JSON data must be encoded as UTF-8, UTF-16 or UTF-32. The JSON decoder can determine the encoding by examining the first four octets of the byte stream:
It sounds like the server is encoding data in some illegal encoding (ISO-8859-1, windows-1252, etc.)
我在使用 Notepad2 保存 JSON 文件后得到了这个,所以我必须使用 Notepad++ 打开它,然后说“转换为 UTF-8”。然后就成功了。
I got this after saving the JSON file using Notepad2, so I had to open it with Notepad++ and then say "Convert to UTF-8". Then it worked.
如果有机会对其他人有帮助,我会分享一个相关的轶事。
我在通过 PowerShell 集成脚本环境 (ISE) 运行 PowerShell 脚本时遇到了这个确切的错误(无效的 UTF-8 中间字节 0x3f)。在 ISE 外部执行的相同脚本可以正常工作。该代码使用 Confluence v3 和 v5.x REST API,并且此错误记录在 Confluence v5.x 服务器上 - 可能是因为 ISE 以某种方式干扰了请求。
On the off chance it may help others I'll share a related anecdote.
I encountered this exact error (Invalid UTF-8 middle byte 0x3f) running a PowerShell script via the PowerShell Integrated Script Environment (ISE). The identical script, executed outside the ISE, works fine. The code uses the Confluence v3 and v5.x REST APIs and this error is logged on the Confluence v5.x server - presumably because the ISE somehow mucks with the request.
这个遮阳篷解决了我的问题。下面是它的副本:
这是一个 JVM 参数,可以添加到例如独立的 JBoss 或从 Eclipse 运行的 JBoss。
就我而言,这个问题仅在我的团队成员的一台计算机上单独发生。所有其他人都在工作,没有这个问题。
This awnser solved my problem. Below is a copy of it:
This is a JVM argument which could be added, for example, either to JBoss standalone or JBoss running from Eclipse.
In my case, this problem happened isolatelly on only one of my team people's computer. All the others was working without this problem.
我在不同平台之间遇到了这个不一致的问题,因为我从 Mapper 获取 JSON 作为字符串并自己编写。有时它以 ansi 格式写入文件,有时则以 UTF8 格式正确写入文件。我转而
让 Mapper 执行文件操作,并且它开始正常工作。
I had this problem inconsistently between different platforms, as I got JSON as String from Mapper and did the writing myself. Sometimes it went into file as ansi and other times correctly as UTF8. I switched to
letting Mapper do the file operations, and it started working fine.
客户端文本协议
springboot控制器代码如下:
当我在loopback接口上调试时,它工作正常。当通过 bat 命令在互联网服务器上部署时,我收到错误
添加 jvm 争论对我有用。
java -Dfile.encoding=UTF-8
client text protocol
a springboot Controller code as below:
when i debug on loopback interface,it works ok. while deploy on internet server via bat command, i got an error
add a jvm arguement works for me.
java -Dfile.encoding=UTF-8