Cannot deserialize instance of `java.lang.Boolean` out of START_OBJECT token
Asked Answered
L

4

7

Here is my Controller mapping for put request:

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

When I run the following request from POSTMAN:

http://localhost:8082/microblog/api/voteForPostByUser?postId=5d564a2638195729900df9a6&userId=5

Request Body:
{
        "vote" : true
    }

I get the following exception

"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 

I is probably something simple, but I don't get what am I missing ?

Layamon answered 16/8, 2019 at 8:20 Comment(3)
at first sight, the issue is that you are trying to convert the entire body request to a Boolean while the actual Boolean is just the inner field. If you create a class VoteRequest with a Boolean vote field in it, it should workRanunculus
Possible duplicate of Cannot deserialize instance of int[] out of START_OBJECT tokenCalcar
If so, this one should work: 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) { ... }Layamon
C
3

I try on postman using true or false like this.It's ok.

enter image description here

Ciliata answered 16/8, 2019 at 9:49 Comment(0)
C
5

You just need to send true or false as the body of the request, no need for curly braces or key-value structure

Cyclograph answered 16/8, 2019 at 8:37 Comment(0)
A
5

Create a new class for your payload:

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

and use it as your 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);  
}
Anglocatholic answered 16/8, 2019 at 8:41 Comment(1)
Thanks. I also stuck in the same problem..Used boolean instead of Boolean so always use the non -primitve or Wrapper class instead of Primitive once.Thinskinned
C
3

I try on postman using true or false like this.It's ok.

enter image description here

Ciliata answered 16/8, 2019 at 9:49 Comment(0)
A
2

You expect a boolean from your @RequestBody Boolean vote however JSON sends text. You can either use the Payload class as suggested already but you can also simply change your controller to expect a String like this @RequestBody String vote and convert that string into boolean using Boolean.valueOf(vote) to be able to use it where you need it.

Arriaga answered 9/11, 2019 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.