如果字段的值为空,那么如何将Jackson配置为在序列化期间忽略该字段值。
例如:
public class SomeClass {
// what jackson annotation causes jackson to skip over this value if it is null but will
// serialize it otherwise
private String someValue;
}
如果字段的值为空,那么如何将Jackson配置为在序列化期间忽略该字段值。
例如:
public class SomeClass {
// what jackson annotation causes jackson to skip over this value if it is null but will
// serialize it otherwise
private String someValue;
}
当前回答
对于Jackson 2.5使用:
@JsonInclude(content=Include.NON_NULL)
其他回答
试试这个-
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class XYZ {
protected String field1;
protected String field2;
}
对于非空值(在getter/class级别)-
@JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY)
在Jackson 2.x中,使用:
@JsonInclude(JsonInclude.Include.NON_NULL)
这已经困扰了我一段时间,我终于找到了问题所在。问题是由于错误的导入。早些时候我一直在使用
com.fasterxml.jackson.databind.annotation.JsonSerialize
已被弃用。只需将导入替换为
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
并将其用作
@JsonSerialize(include=Inclusion.NON_NULL)
就我而言
@JsonInclude(Include.NON_EMPTY)
让它发挥作用。
对于Jackson>1.9.11和<2.x,请使用@JsonSerialize注释来实现:
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)