JSON parse error: Cannot deserialize instance of `int` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc
代码程序:
@PostMapping("selectById") @ResponseBody public Result selectById(@RequestBody int id) { Result result =new Result(); List<User> list = userService.selectById(id); if(list.size()==1){ result.setCode("000"); result.setMsg("success"); result.setData(list); }else if(list.size()==0){ result.setCode("E01"); result.setMsg("输入的id找不到有效用户"); result.setData(null); }else { result.setCode("E02"); result.setMsg("输入的id找到大于1个有效用户"); result.setData(list); } return result; }
请求内容:
POST http://localhost:8080/user/selectById
POST data:
{
"id":300
}
返回信息:
{
: "timestamp":"2019-02-08T14:05:17.430+0000",
: "status":400,
: "error":"Bad Request",
: "message":"JSON parse error: Cannot deserialize instance of `int` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `int` out of START_OBJECT token\n at [Source: (PushbackInputStream); line: 1, column: 1]",
: "path":"/user/selectById"
}
用的是post的方式,传的入参是int id, 不是一个对象。
需要改成对象,然后获取对象的id
@ApiOperation(value = "根据ID查询用户的信息") @PostMapping("selectById") @ResponseBody public Result selectById(@RequestBody User user) { Result result =new Result(); List<User> list = userService.selectById(user.getId()); if(list.size()==1){ result.setCode("000"); result.setMsg("success"); result.setData(list); }else if(list.size()==0){ result.setCode("E01"); result.setMsg("输入的id找不到有效用户"); result.setData(null); }else { result.setCode("E02"); result.setMsg("输入的id找到大于1个有效用户"); result.setData(list); } return result; }
![](https://images.cnblogs.com/cnblogs_com/qianjinyan/910455/o_jasmine%20love.gif)