需要帮助从 Spring-boot 使用 Jackson 反序列化 json
Need help to deserialize json with Jackson from Spring-boot
我正在尝试 POST Json 到我的 REST 服务,但是我遇到了字符集错误,我做了一个非常简单的示例来重现我的错误。
简单模型人物
public class Person {
public Person() {}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
简单的休息控制器:
@RestController
public class PersonController {
@RequestMapping(value={"/person"}, method=RequestMethod.POST, consumes="application/json", produces="application/json")
public @ResponseBody ResponseEntity<String> newPerson(@RequestBody Person person) {
return new ResponseEntity<String>(person.getName(), HttpStatus.OK);
}
}
当我POSTjson:
{
"name":"Joao"
}
工作正常,我的响应主体上有 200 OK 和 "Joao"。
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Application-Context: application
Content-Type: application/json;charset=UTF-8
Content-Length: 4
Date: Mon, 25 May 2015 19:09:56 GMT
Joao
但是如果我尝试 json 和拉丁字符
{
"name":"João"
}
我收到以下错误:
HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
X-Application-Context: application
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 25 May 2015 19:12:56 GMT
Connection: close
{"timestamp":1432581176997,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read JSON: Invalid UTF-8 middle byte 0x6f\n at [Source: java.io.PushbackInputStream@10b0d4cd; line: 2, column: 14] (through reference chain: com.example.model.Person[\"name\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 middle byte 0x6f\n at [Source: java.io.PushbackInputStream@10b0d4cd; line: 2, column: 14] (through reference chain: com.example.model.Person[\"name\"])","path":"/person"}
我正在使用 spring-boot 1.2。3.RELEASE 它依赖于 jackson 2.5.1。
我找不到解决这个问题的方法,有什么建议吗?
问题已解决 SOAP 存在错误 UI
我正在尝试 POST Json 到我的 REST 服务,但是我遇到了字符集错误,我做了一个非常简单的示例来重现我的错误。
简单模型人物
public class Person {
public Person() {}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
简单的休息控制器:
@RestController
public class PersonController {
@RequestMapping(value={"/person"}, method=RequestMethod.POST, consumes="application/json", produces="application/json")
public @ResponseBody ResponseEntity<String> newPerson(@RequestBody Person person) {
return new ResponseEntity<String>(person.getName(), HttpStatus.OK);
}
}
当我POSTjson:
{
"name":"Joao"
}
工作正常,我的响应主体上有 200 OK 和 "Joao"。
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-Application-Context: application
Content-Type: application/json;charset=UTF-8
Content-Length: 4
Date: Mon, 25 May 2015 19:09:56 GMT
Joao
但是如果我尝试 json 和拉丁字符
{
"name":"João"
}
我收到以下错误:
HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
X-Application-Context: application
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 25 May 2015 19:12:56 GMT
Connection: close
{"timestamp":1432581176997,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read JSON: Invalid UTF-8 middle byte 0x6f\n at [Source: java.io.PushbackInputStream@10b0d4cd; line: 2, column: 14] (through reference chain: com.example.model.Person[\"name\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 middle byte 0x6f\n at [Source: java.io.PushbackInputStream@10b0d4cd; line: 2, column: 14] (through reference chain: com.example.model.Person[\"name\"])","path":"/person"}
我正在使用 spring-boot 1.2。3.RELEASE 它依赖于 jackson 2.5.1。
我找不到解决这个问题的方法,有什么建议吗?
问题已解决 SOAP 存在错误 UI