更新时间:2023-01-17 08:16:53
请记住,默认情况下,杰克逊从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中的
.这些基本上是firstName
,lastName
,active
.
in the JSON. These are basically firstName
, lastName
, active
.
您将获得以下JSON
You get the following JSON
{ "firstName": "Test", "lastName": "1", "isActive": 1 }
因此,已映射firstName
和lastName
,但是您没有名为isActive
的属性.
So firstName
and lastName
are mapped, but you don't have a property named isActive
.
Jackson依赖于Java Bean的命名约定及其访问器(获取器)和更改器(设置器).对于类似
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;