当(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)
答案 0 :(得分:28)
JSON数据必须编码为UTF-8,UTF-16或UTF-32。 JSON解码器可以通过字节流的examining the first four octets来确定编码:
00 00 00 xx UTF-32BE
00 xx 00 xx UTF-16BE
xx 00 00 00 UTF-32LE
xx 00 xx 00 UTF-16LE
xx xx xx xx UTF-8
听起来服务器正在以某种非法编码(ISO-8859-1,windows-1252等)编码数据
答案 1 :(得分:16)
我在Java客户端应用程序中遇到了这个异常,我正在序列化像这样的JSON
String json = mapper.writeValueAsString(contentBean);
在服务器端我使用Spring Boot作为REST端点。 例外是:
嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无效的UTF-8起始字节0xaa
我的问题是,我没有在HTTP客户端上设置正确的编码。 这解决了我的问题:
updateRequest.setHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity entity= new StringEntity(json, "UTF-8");
updateRequest.setEntity(entity);
答案 2 :(得分:2)
我在使用Notepad2保存JSON文件后得到了这个,所以我不得不用Notepad ++打开它,然后说&#34;转换为UTF-8&#34;。然后它奏效了。
答案 3 :(得分:0)
我在不同平台之间不一致地遇到这个问题,因为我从Mapper获得了JSON作为String并且自己写了这个。有时它作为ansi进入文件,其他时候正确地作为UTF8。我切换到了
mapper.writeValue(file, data);
让Mapper执行文件操作,它开始正常工作。
答案 4 :(得分:0)
如果有机会可以帮助别人,我会分享相关的轶事。
我遇到了通过PowerShell 集成脚本环境(ISE) 无效的UTF-8中间字节0x3f ) >。在ISE外执行的相同脚本运行正常。代码使用Confluence v3和v5.x REST API,并在Confluence v5.x服务器上记录此错误 - 可能是因为ISE以某种方式干扰了请求。
答案 5 :(得分:0)
客户端文本协议
POST http://127.0.0.1/bom/create HTTP/1.1
Content-Type: application/json
User-Agent: PostmanRuntime/7.25.0
Accept: */*
Postman-Token: 50ecfbfe-741f-4a2b-a3d3-cdf162ada27f
Host: 127.0.0.1
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 405
{
"fwoid": 1,
"list": [
{
"bomIndex": "10001",
"desc": "带GH 1.25 13pin 公座带针 白色",
"pn": "084.0001.0036",
"preUse": 1,
"type": "追觅 除螨仪-开关PCB组件"
},
{
"bomIndex": "10002",
"desc": "紫米音箱-商品码标签",
"pn": "Z.08.013.0051",
"preUse": 1,
"type": "E060A0302301"
}
]
}
HTTP/1.1 200 OK
Connection: keep-alive
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Content-Type: application/json
Date: Mon, 01 Jun 2020 11:23:42 GMT
Content-Length: 40
{"code":"0","message":"BOM保存成功"}
springboot控制器代码如下:
@PostMapping("/bom/create")
@ApiOperation(value = "保存BOM")
@BusinessOperation(module = "BOM",methods = "解析BOM")
public JsonResult save(@RequestBody BOMSaveQuery query)
{
return bomService.saveBomList(query);
}
当我在环回接口上调试时,它工作正常。通过bat命令在Internet服务器上部署时,出现错误
ServletInvocableHandlerMethod - Could not resolve parameter [0] in public XXXController.save(com.h2.mes.query.BOMSaveQuery): JSON parse error: Invalid UTF-8 middle byte 0x3f; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 middle byte 0x3f
at [Source: (PushbackInputStream); line: 9, column: 32] (through reference chain: com.h2.mes.query.BOMSaveQuery["list"]->java.util.ArrayList[0]->com.h2.mes.vo.BOMVO["type"])
2020-06-01 15:37:50.251 MES [XNIO-1 task-13] WARN o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Invalid UTF-8 middle byte 0x3f; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 middle byte 0x3f
at [Source: (PushbackInputStream); line: 9, column: 32] (through reference chain: com.h2.mes.query.BOMSaveQuery["list"]->java.util.ArrayList[0]->com.h2.mes.vo.BOMVO["type"])]
2020-06-01 15:37:50.251 MES [XNIO-1 task-13] DEBUG o.s.web.servlet.DispatcherServlet - Completed 400 BAD_REQUEST
2020-06-01 15:37:50.251 MES [XNIO-1 task-13] DEBUG o.s.web.servlet.DispatcherServlet - "ERROR" dispatch for POST "/error", parameters={}
为我添加一个jvm争论。 java -Dfile.encoding = UTF-8
答案 6 :(得分:0)
This awnser解决了我的问题。下面是它的副本:
确保使用-Dfile.encoding = UTF-8启动JVM。你的JVM 默认为操作系统字符集
这是一个JVM参数,可以将其添加到例如独立的JBoss或从Eclipse运行的JBoss中。
就我而言,此问题仅在团队成员的一台计算机上发生。其他所有人都在工作,没有这个问题。