Jackson:将空字符串反序列化为空字符串 [英] Jackson: deserializing null Strings as empty Strings

查看:55
本文介绍了Jackson:将空字符串反序列化为空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类,由 Jackson 映射(简化版):

I have the following class, that is mapped by Jackson (simplified version):

public class POI {
    @JsonProperty("name")
    private String name;
}

在某些情况下,服务器返回 "name": null 然后我想将 name 设置为空的 Java 字符串.

In some cases the server returns "name": null and I would like to then set name to empty Java String.

是否有任何 Jackson 注释,或者我应该在我的 getter 中检查 null 并在属性为 null 时返回空字符串?

Is there any Jackson annotation or should I just check for the null inside my getter and return empty string if the property is null?

推荐答案

您可以在默认构造函数中或在声明时设置它:

You can either set it in the default constructor, or on declaration:

public class POI {
    @JsonProperty("name")
    private String name; 

    public POI() {
        name = "";
    }
}

public class POI {
    @JsonProperty("name")
    private String name = "";
} 

这篇关于Jackson:将空字符串反序列化为空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆