如果字段的值为空,如何将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;
}
To suppress serializing properties with null values using Jackson >2.0, you can configure the ObjectMapper
directly, or make use of the @JsonInclude
annotation:
mapper.setSerializationInclusion(Include.NON_NULL);
或者:
@JsonInclude(Include.NON_NULL)
class Foo
{
String bar;
}
或者,可以在getter中使用@JsonInclude
,以便在值不为null时显示属性.
更完整的示例在my answer到How to prevent null values inside a Map and null fields inside a bean from getting serialized through Jackson中提供.