Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Post request for boolean field sends false by default

Hi I am sending a JSON Post request using the FireFox RestClient.

My JSON Request is as below:

 { "firstName": "Test", "lastName": "1", "isActive": 1 } 

My POJO has isActive field as below

  private boolean isActive; 

My Controller is define as below

@RequestMapping(method = {RequestMethod.POST,                                   RequestMethod.PUT}, value = "/save") public ResponseEntity<RestResponse> save(       @RequestBody POJOUserDetails userDetails, WebRequest request){ 

In my POJO, when I check the value of isActive, it is false no matter what I send. I tried below value in my JSON request

"isActive": 1 "isActive": true  "isActive": "true" "isActive": "" "isActive": null "isActive": false 

All of above sends false in my controller. Please help. Thanks

Adding POJO details

@JsonIgnoreProperties(ignoreUnknown = true) @JsonSerialize(include=Inclusion.NON_EMPTY) public class POJOUserDetails { private String firstName; private String lastName; private boolean isActive;  public boolean isActive() {     return isActive; } public void setActive(boolean isActive) {     this.isActive = isActive;        }      public String getFirstName() {     return firstName; } public void setFirstName(String firstName) {     this.firstName = firstName; } public String getLastName() {     return lastName; } public void setLastName(String lastName) {     this.lastName = lastName; }    } 
like image 372
NewQueries Avatar asked Feb 20 '14 16:02

NewQueries


People also ask

Can I send boolean in JSON?

Boolean is one of the types supported by json: https://www.json.org/json-en.html and the expected values are true or false, without quotes.

How do you pass a boolean value in Postman JSON?

So even if you send a parameter like “active=true”, it is still a string, this is how the HTTP protocol works. Hope this helps clarify the mystery. var bflag = Boolean(“true”); var bflag1 = Boolean(“false”);

Is JSON boolean case sensitive?

Note that in JSON, true and false are lower case, whereas in Python they are capitalized ( True and False ).


2 Answers

Remember that Jackson, by default, determines the property name from either the getter or setter (the first that matches).

To deserialize an object of type POJOUserDetails, Jackson will look for three properties

public void setFirstName(String firstName) {  public void setLastName(String lastName) {  public void setActive(boolean isActive) { 

in the JSON. These are basically firstName, lastName, active.

You get the following JSON

{ "firstName": "Test", "lastName": "1", "isActive": 1 } 

So firstName and lastName are mapped, but you don't have a property named isActive.

Jackson depends on Java Bean naming conventions with their accessors (getters) and mutators (setters). For a field like

private boolean isActive; 

the appropriate setter/getter names are

public boolean getIsActive() {     return isActive; }  public void setIsActive(boolean isActive) {     this.isActive = isActive; } 

So you have two possible solutions. Change your getter/setter as shown above or annotate your field with @JsonProperty so that Jackson uses the field name to determine the property name

@JsonProperty private boolean isActive; 
like image 87
Sotirios Delimanolis Avatar answered Sep 24 '22 07:09

Sotirios Delimanolis


When you are using libraries like lombok to generate getters and setters, don't add 'is' with the field name if the field type is boolean. Because Jackson uses default naming bean convention of java and adds 'is' while setting fields. so adding 'is' makes the field mapping wrong

like image 25
Chinmay Biswal Avatar answered Sep 23 '22 07:09

Chinmay Biswal