JSON Post request for boolean field sends false by default
Solution 1:
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;
Solution 2:
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