JsonMappingException:无法从 START_OBJECT 令牌反序列化 java.lang.Integer 的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37471005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token
提问by user2581426
I wanted to write a small and simple REST service using Spring Boot. Here is the REST service code:
我想使用 Spring Boot 编写一个小而简单的 REST 服务。这是 REST 服务代码:
@Async
@RequestMapping(value = "/getuser", method = POST, consumes = "application/json", produces = "application/json")
public @ResponseBody Record getRecord(@RequestBody Integer userId) {
Record result = null;
// Omitted logic
return result;
}
The JSON object I sent is the following:
我发送的 JSON 对象如下:
{
"userId": 3
}
And here is the exception I got:
这是我得到的例外:
WARN 964 --- [ XNIO-2 task-7] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.Integer out of START_OBJECT token at [Source: java.io.PushbackInputStream@12e7333c; line: 1, column: 1]; nested exception is com.fasterxml.Hymanson.databind.JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token at [Source: java.io.PushbackInputStream@12e7333c; line: 1, column: 1]
警告 964 --- [XNIO-2 task-7] .wsmsDefaultHandlerExceptionResolver:无法读取 HTTP 消息:org.springframework.http.converter.HttpMessageNotReadableException:无法读取文档:无法从 START_OBJECT 中反序列化 java.lang.Integer 的实例[来源:java.io.PushbackInputStream@12e7333c; 行:1,列:1];嵌套异常是 com.fasterxml.Hymanson.databind.JsonMappingException:无法从 [来源:java.io.PushbackInputStream@12e7333c; 的 START_OBJECT 令牌中反序列化 java.lang.Integer 的实例;行:1,列:1]
采纳答案by Ali Dehghani
Obviously Hymanson can not deserialize the passed JSON into an Integer
. If you insist to send a JSON representation of a Userthrough the request body, you should encapsulate the userId
in another bean like the following:
显然 Hymanson 无法将传递的 JSON 反序列化为Integer
. 如果您坚持通过请求正文发送用户的 JSON 表示,则应将其封装userId
在另一个 bean 中,如下所示:
public class User {
private Integer userId;
// getters and setters
}
Then use that bean as your handler method argument:
然后使用该 bean 作为您的处理程序方法参数:
@RequestMapping(...)
public @ResponseBody Record getRecord(@RequestBody User user) { ... }
If you don't like the overhead of creating another bean, you could pass the userId
as part of Path Variable, e.g. /getuser/15
. In order to do that:
如果您不喜欢创建另一个 bean 的开销,您可以将其userId
作为Path Variable 的一部分传递,例如/getuser/15
. 为了做到这一点:
@RequestMapping(value = "/getuser/{userId}", method = POST, produces = "application/json")
public @ResponseBody Record getRecord(@PathVariable Integer userId) { ... }
Since you no longer send a JSON in the request body, you should remove that consumes
attribute.
由于您不再在请求正文中发送 JSON,您应该删除该consumes
属性。
回答by Javier Sanchez C
Perhaps you are trying to send a request with JSON text in its body from a Postman client or something similar like this:
也许您正在尝试从 Postman 客户端或类似的东西发送一个在其正文中包含 JSON 文本的请求:
{
"userId": 3
}
This cannot be deserialized by Hymanson since this is not an Integer (it seems to be, but it isn't). An Integer object from java.lang Integer is a little more complex.
这不能被Hyman逊反序列化,因为这不是一个整数(似乎是,但实际上不是)。来自 java.lang Integer 的 Integer 对象稍微复杂一些。
For your Postman request to work, simply put (without curly braces { }):
为了让您的 Postman 请求工作,只需输入(不带花括号 { }):
3