Java Jackson序列化:忽略空值(或null)

Java Jackson序列化:忽略空值(或null),java,json,jackson,Java,Json,Jackson,我目前正在使用jackson 2.1.4,在将对象转换为JSON字符串时忽略字段时遇到了一些问题 下面是我的类,它充当要转换的对象: public class JsonOperation { public static class Request { @JsonInclude(Include.NON_EMPTY) String requestType; Data data = new Data(); public static class Data {

我目前正在使用jackson 2.1.4,在将对象转换为JSON字符串时忽略字段时遇到了一些问题

下面是我的类,它充当要转换的对象:

public class JsonOperation {

public static class Request {
    @JsonInclude(Include.NON_EMPTY)
    String requestType;
    Data data = new Data();

    public static class Data {
        @JsonInclude(Include.NON_EMPTY)
        String username;
        String email;
        String password;
        String birthday;
        String coinsPackage;
        String coins;
        String transactionId;
        boolean isLoggedIn;
    }
}

public static class Response {
    @JsonInclude(Include.NON_EMPTY)
    String requestType = null;
    Data data = new Data();

    public static class Data {
        @JsonInclude(Include.NON_EMPTY)
        enum ErrorCode { ERROR_INVALID_LOGIN, ERROR_USERNAME_ALREADY_TAKEN, ERROR_EMAIL_ALREADY_TAKEN };
        enum Status { ok, error };

        Status status;
        ErrorCode errorCode;
        String expiry;
        int coins;
        String email;
        String birthday;
        String pictureUrl;
        ArrayList <Performer> performer;
    }
}
}
以下是输出:

{"data":{"birthday":null,"coins":null,"coinsPackage":null,"email":null,"username":"Vincent","password":"test","transactionId":null,"isLoggedIn":false},"requestType":"login"}
如何避免这些空值?我只想获取“订阅”所需的信息

这正是我想要的结果:

{"data":{"username":"Vincent","password":"test"},"requestType":"login"}

我还尝试了@JsonInclude(Include.NON_NULL)并将所有变量设置为NULL,但也没有成功!谢谢你们的帮助,伙计们

注释放错了位置-它需要在类上,而不是字段上。i、 e:

@JsonInclude(Include.NON_NULL) //or Include.NON_EMPTY, if that fits your use case 
public static class Request {
  // ...
}
如注释中所述,在2.x以下的版本中,此注释的语法为:

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) // or JsonSerialize.Inclusion.NON_EMPTY
另一个选项是直接配置
ObjectMapper
,只需调用
mapper.setSerializationInclusion(Include.NON_NULL)


(为了记录在案,我认为这个答案的流行表明这个注释应该适用于每个字段,@fasterxml)

您还可以设置全局选项:

objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

你也可以尝试使用

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
如果您正在处理版本低于2+(1.9.5)的jackson,我测试了它,您可以轻松地在类之上使用此注释。不是为属性指定的,只是为了类删除。

或者您可以使用GSON[],其中这些空字段将自动删除

SampleDTO.java

public class SampleDTO {

    String username;
    String email;
    String password;
    String birthday;
    String coinsPackage;
    String coins;
    String transactionId;
    boolean isLoggedIn;

    // getters/setters
}
import com.google.gson.Gson;

public class Test {

    public static void main(String[] args) {
        SampleDTO objSampleDTO = new SampleDTO();
        Gson objGson = new Gson();
        System.out.println(objGson.toJson(objSampleDTO));
    }
}
Test.java

public class SampleDTO {

    String username;
    String email;
    String password;
    String birthday;
    String coinsPackage;
    String coins;
    String transactionId;
    boolean isLoggedIn;

    // getters/setters
}
import com.google.gson.Gson;

public class Test {

    public static void main(String[] args) {
        SampleDTO objSampleDTO = new SampleDTO();
        Gson objGson = new Gson();
        System.out.println(objGson.toJson(objSampleDTO));
    }
}
输出:

{"isLoggedIn":false}
我对jackson 2.x使用了gson-2.2.4

@JsonInclude(JsonInclude.Include.NON_NULL)

就在字段前面。

您需要添加
import com.fasterxml.jackson.annotation.JsonInclude

在波乔之上

如果您有嵌套的POJO,那么

@JsonInclude(JsonInclude.Include.NON_NULL)
需要添加每个值

注:
JAXRS(Jersey)会自动处理这种场景2.6及以上版本。

我最近在版本2.6.6中遇到了类似的问题

@JsonInclude(JsonInclude.Include.NON_NULL)
在字段级或类级使用上述注释未按预期工作。在我应用注释的地方,POJO是可变的。当我将POJO的行为更改为不可变时,注释发挥了它的魔力

我不确定这个lib的新版本或以前版本是否有类似的行为,但对于2.6.6,您肯定需要有不可变的POJO才能使注释正常工作

objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
在ObjectMapper中直接在全局级别设置序列化包含的各种答案中提到的上述选项同样有效,但我更喜欢在类或字段级别控制它


因此,如果您希望在JSON序列化时忽略所有空字段,则在类级别使用注释;如果您希望在类中只忽略少数字段,则在这些特定字段上使用注释。这样,如果您想更改特定响应的行为,它的可读性更高,易于维护。

如果您想从序列化中排除布尔类型,下面的代码可能会有所帮助:

@JsonInclude(JsonInclude.Include.NON_ABSENT)

真的没有办法让include.NON_NULL注释工作吗?还是非紧急的?因为我知道哪一个是空的,但这取决于情况。对于每个要序列化/反序列化的对象,我都使用相同的“JsonOperation”类,并且我只根据情况初始化需要使用的变量。这是一种很好的方法吗?或者我应该创建几个只包含我需要的变量的类(这样我就不必使用任何注释,因为永远不会有空/空变量)。语法在最新版本中已更改为@JsonSerialize(include=JsonSerialize.Inclusion.NON_null)和@JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY如果您同时需要非null和非空,请使用@JsonSerialize(include=JsonSerialize.Inclusion.NON_默认值)在类之前或其工作的属性之前使用@JsonSerialize(include=include.NON_null)…@drewmoore请再次检查,
@JsonInclude(JsonSerialize.include.NON_null)
应该是
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
而且这也是旧的不推荐的方式。所以你应该写“在2.x以下的版本中”,而不是“在2.x+版本中”。2+使用
@JsonInclude(include.NON_NULL)
JsonSerialize的include属性不支持JSONinclude,就像我说的:版本低于2+(1.9.5)的jackson问题是关于特定版本2.1.4,当使用旧的org.codehaus.jackson.annotate包时,是否有类似的功能?是的,您可以在getter方法示例private int id上添加;@JsonIgnore public int getId(){return id;}与现有应答器名称相同,与现有应答器名称相同