【问题标题】:Cannot deserialize instance of `java.lang.Boolean` out of START_OBJECT token无法从 START_OBJECT 令牌中反序列化 `java.lang.Boolean` 的实例
【发布时间】: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


【解决方案1】:

你只需要发送truefalse作为请求体,不需要花括号或键值结构

【讨论】:

    【解决方案2】:

    为您的有效负载创建一个新类:

      class Payload {
        Boolean vote;
        // maybe some getters/setter here
      }
    

    并将其用作您的RequestBody

    @PutMapping("/voteForPostByUser")
    public String vote(@RequestParam(value = "postId", required = true) String postId, @RequestParam(value = "userId", required = true) Integer userId, @RequestBody Payload payload) {
        boolean vote = payload.vote; //or payload.getVote()
        BlogPostVoteDTO blogPostVoteDTO = new BlogPostVoteDTO(postId, userId, vote);
        return this.blogPostService.updateBlogPostVotes(blogPostVoteDTO);  
    }
    

    【讨论】:

      【解决方案3】:

      我用这样的真假试穿邮递员。没关系。

      【讨论】:

        【解决方案4】:

        您希望 @RequestBody Boolean vote 提供布尔值,但 JSON 发送文本。您可以按照已经建议的方式使用 Payload 类,但您也可以简单地将控制器更改为期望像 @RequestBody String vote 这样的字符串,并使用 Boolean.valueOf(vote) 将该字符串转换为布尔值,以便能够在需要的地方使用它。

        【讨论】:

          猜你喜欢
          • 2019-10-25
          • 2019-03-06
          • 2021-09-05
          • 2020-07-22
          • 2019-06-01
          • 2019-02-12
          • 2014-01-17
          • 2013-10-23
          相关资源
          最近更新 更多