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

Posted 夏天里的Jasmine

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSON parse error: Cannot deserialize instance of `int` out of START_OBJECT token; nested exception i相关的知识,希望对你有一定的参考价值。

代码程序:

 

@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 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;
	}

  










以上是关于JSON parse error: Cannot deserialize instance of `int` out of START_OBJECT token; nested exception i的主要内容,如果未能解决你的问题,请参考以下文章

Ajax提交数据SpringBoot后台报错“HttpMessageNotReadableException: JSON parse error: Cannot construct instance“

JSON parse error: Cannot deserialize value of type `java.util.Date` from String

JSON parse error: Cannot deserialize instance of `java.util.ArrayList<java.lang.Long>` out of START_

JSON parse error: Cannot deserialize value of type `java.util.Date` from String

解决日期转换异常 JSON parse error: Cannot deserialize value of type `java.util.Date` from String

解决日期转换异常 JSON parse error: Cannot deserialize value of type `java.util.Date` from String