Jackson Deserialiser 忽略字段上的 @JsonSetter(nulls = Nulls.SKIP) 注释,始终设置空值
当通过我们的内部 API 发送 json 请求来创建新实体时,Jackson Json 反序列化器始终将 CountryGroup 设置为 null,即使该字段使用 @JsonSetter(nulls = Nulls.SKIP) 进行注释。当实体在 Spring/Hibernate 提交到数据库之前经过验证(因为字段用 @NotNull 注释)时,这会引发异常。
(我简化了请求和实体,以便更容易理解)
When a json request is sent to create a new Entity via our internal API the Jackson Json deserializer always sets the countryGroup as null even though the field is annotated with @JsonSetter(nulls = Nulls.SKIP). This throws an exception when the entity is validated ( as field is annotated with @NotNull) before committed to the DB by Spring/Hibernate.
(I have simplified the request and entity to make it easier to understand)????
request looks like:
{
//...other fields in actual entity
"countryGroup": null
}
with an entity class defined like this:
@Entity
public class Entity{
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id;
public Long getId() {
return id;
}
//...other fields in actual entity
@JsonSetter(nulls = Nulls.SKIP)
@NotNull
@Column(name = "country_group", length = 64)
private String countryGroup = "";// default value
public String getCountryGroup() {
return countryGroup;
}
}
As previously mentioned there are other fields being set properly but the countryGroup
filed always gets set to null. We are using Jackson 2.9 so this should be supported. As far as I can see I have set the annotation correctly, could there any other configuration that could be overriding this? Or some other reason as to why this does not work.
I have tried to debug and I can see the default ""
value for countryGroup
set by the default constructor but this is always overridden when the property exists in the json object, if it is not in the json it keep the default value.
[EDIT] Fixed typo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论