【发布时间】:2019-12-22 13:47:21
【问题描述】:
这是我对 put 请求的控制器映射:
@PutMapping("/voteForPostByUser")
public String vote(@RequestParam(value = "postId", required =
true) String postId, @RequestParam(value = "userId", required = true)
Integer userId, @RequestBody Boolean vote) {
BlogPostVoteDTO blogPostVoteDTO = new BlogPostVoteDTO
(postId, userId, vote);
return
this.blogPostService.updateBlogPostVotes(blogPostVoteDTO);
}
当我从 POSTMAN 运行以下请求时:
http://localhost:8082/microblog/api/voteForPostByUser?postId=5d564a2638195729900df9a6&userId=5
Request Body:
{
"vote" : true
}
我得到以下异常
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Cannot deserialize instance of
`java.lang.Boolean` out of START_OBJECT token; nested exception is
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot
deserialize instance of `java.lang.Boolean` out of START_OBJECT token\n
at [Source: (PushbackInputStream); line: 1, column: 1]",
"trace":
"org.springframework.http.converter.HttpMessageNotReadableException: JSON
parse error: Cannot deserialize instance of `java.lang.Boolean` out of
START_OBJECT token; nested exception is
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot
deserialize instance of `java.lang.Boolean` out of START_OBJECT token\n
at [Source: (PushbackInputStream); line: 1, column: 1]\r\n\tat
我可能很简单,但我不明白我错过了什么?
【问题讨论】:
-
乍一看,问题在于您试图将整个正文请求转换为布尔值,而实际的布尔值只是内部字段。如果您创建一个带有布尔投票字段的类 VoteRequest,它应该可以工作
-
如果是这样,这个应该可以工作:localhost:8082/microblog/api/…@PutMapping("/voteForPostByUser") public String vote(@RequestParam(value = "postId", required = true) String postId, @RequestParam( value = "userId", required = true) Integer userId, @RequestParam(value = "vote", required = true) Boolean vote) { ... }
标签: java json rest spring-boot jackson-databind