Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JsonMappingException:无法反序列化START\u对象标记之外的java.lang.Integer实例_Java_Spring_Spring Boot_Jackson - Fatal编程技术网

JsonMappingException:无法反序列化START\u对象标记之外的java.lang.Integer实例

JsonMappingException:无法反序列化START\u对象标记之外的java.lang.Integer实例,java,spring,spring-boot,jackson,Java,Spring,Spring Boot,Jackson,我想用SpringBoot编写一个小而简单的REST服务。 以下是REST服务代码: @Async @RequestMapping(value = "/getuser", method = POST, consumes = "application/json", produces = "application/json") public @ResponseBody Record getRecord(@RequestBody Integer userId) { Record result

我想用SpringBoot编写一个小而简单的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;
}
我发送的JSON对象如下所示:

{
    "userId": 3
}
public class User {
    private Integer userId;
    // getters and setters
}
这是我得到的一个例外:

警告964---[XNIO-2任务-7] .w.s.m.s.DefaultHandlerExceptionResolver:无法读取HTTP 信息: org.springframework.http.converter.httpMessageNodeTableException: 无法读取文档:无法反序列化的实例 java.lang.Integer在[Source: java.io。PushbackInputStream@12e7333c;行:1,列:1];嵌套 异常为com.fasterxml.jackson.databind.JsonMappingException:Can 未反序列化START\u对象外的java.lang.Integer实例 令牌位于[源代码:java.io]。PushbackInputStream@12e7333c;行:1, 栏目:1]


显然,Jackson无法将传递的JSON反序列化为
整数。如果坚持通过请求主体发送用户的JSON表示,则应将
userId
封装在另一个bean中,如下所示:

{
    "userId": 3
}
public class User {
    private Integer userId;
    // getters and setters
}
然后将该bean用作处理程序方法参数:

@RequestMapping(...)
public @ResponseBody Record getRecord(@RequestBody User user) { ... }
如果您不喜欢创建另一个bean的开销,那么可以将
userId
作为Path变量的一部分传递,例如
/getuser/15
。为此:

@RequestMapping(value = "/getuser/{userId}", method = POST, produces = "application/json")
public @ResponseBody Record getRecord(@PathVariable Integer userId) { ... }

由于您不再在请求正文中发送JSON,因此应删除
消耗的属性。

可能您正在尝试从Postman客户端或类似的方式发送正文中包含JSON文本的请求:

{
 "userId": 3
}
Jackson无法对其进行反序列化,因为它不是整数(看起来是,但不是)。java.lang Integer中的Integer对象稍微复杂一些

对于您的邮递员工作请求,只需输入(不带花括号{}):


我知道问题出在Jackson对象映射器上,但我不知道通过HTTP发送JSON对象。我最初的建议是,问题在于我的JSON对象。谢谢你的回答,阿里·德哈尼!如果不使用PathVariable注释,就没有其他方法在POST方法中发送简单整数?除了Jackson之外,还有其他反序列化技术可以反序列化请求正文中的一个简单整数吗?您可以使用
application/x-www-form-urlencoded
在请求正文中发送一个键值对,而不是
application/json
,例如
userId=15
,如果不是@PathVariable,您可以使用@RequestParam(“userId”)这将是URI中的查询参数,并将在您的服务URI结束时被视为“?userId=15”a。我知道这很晚了,但如何使我的控制器方法发送请求,如
{“userId”:3}
@Gimnath send
{“userId”:“{userIdVariable}}
,用引号括起来,变量名和大括号之间没有空格。