且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

布尔字段的 JSON Post 请求默认发送 false

更新时间:2022-01-10 22:22:21

请记住,默认情况下,Jackson 从 getter 或 setter(第一个匹配的)确定属性名称.

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

要反序列化 POJOUserDetails 类型的对象,Jackson 将查找三个属性

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) {

在 JSON 中.这些基本上是firstNamelastNameactive.

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

你得到以下 JSON

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

因此 firstNamelastName 已映射,但您没有名为 isActive 的属性.

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

Jackson 依赖于 Java Bean 命名约定及其访问器(getter)和修改器(setter).对于像

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

private boolean isActive;

合适的 setter/getter 名称是

the appropriate setter/getter names are

public boolean getIsActive() {
    return isActive;
}

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

所以你有两种可能的解决方案.如上所示更改您的 getter/setter 或使用 @JsonProperty 注释您的字段,以便 Jackson 使用字段名称来确定属性名称

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;