如果字段的值为空,那么如何将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;
}
当前回答
您可以设置application.properties:
spring.jackson.default-property-inclusion=non_null
或application.yaml:
spring:
jackson:
default-property-inclusion: non_null
http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
其他回答
案例一
@JsonInclude(JsonInclude.Include.NON_NULL)
private String someString;
案例二
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String someString;
如果someString为空,则在这两种情况下都将忽略它。如果someString为“”,则只能在第二种情况下忽略。
对于List=null或List.size()=0也是如此
如果您想将此规则添加到Jackson 2.6+中的所有模型,请使用:
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Jackson 2.x+使用
mapper.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.NON_NULL);
对于Jackson>1.9.11和<2.x,请使用@JsonSerialize注释来实现:
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
就我而言
@JsonInclude(Include.NON_EMPTY)
让它发挥作用。