JSON parse error: Cannot deserialize instance of `int` out of START_OBJECT token; nested exception i...

时间:2025-02-08 07:33:35

代码程序:

 

@PostMapping("selectById")
@ResponseBody
public Result selectById(@RequestBody int id) {
   Result result =new Result();
   List<User> list = (id);
   if(()==1){
   ("000");
   ("success");
   (list);
}else if(()==0){
      ("E01");
      ("输入的id找不到有效用户");
      (null);
   }else {
      ("E02");
      ("输入的id找到大于1个有效用户");
      (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 : 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 = (());
		if(()==1){
		("000");
		("success");
		(list);
	}else if(()==0){
			("E01");
			("输入的id找不到有效用户");
			(null);
		}else {
			("E02");
			("输入的id找到大于1个有效用户");
			(list);
		}
		return result;
	}