Jackson Deserialiser 忽略字段上的 @JsonSetter(nulls = Nulls.SKIP) 注释,始终设置空值

发布于 2025-01-20 01:01:58 字数 213 浏览 0 评论 0原文

当通过我们的内部 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文