Ela Revzin
Ela Revzin

Reputation: 103

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

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 ?

Upvotes: 7

Views: 18707

Answers (4)

matel
matel

Reputation: 485

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.

Upvotes: 2

caicai
caicai

Reputation: 46

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

enter image description here

Upvotes: 3

Hakan Dilek
Hakan Dilek

Reputation: 2256

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

Upvotes: 5

Ahmed Abdelhady
Ahmed Abdelhady

Reputation: 749

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

Upvotes: 5

Related Questions