在SpringBoot1.2.3中,我们可以通过属性文件自定义Jackson ObjectMapper。但是,当将对象序列化为JSON字符串时,我没有找到可以设置Jackson忽略空值的属性。
spring.jackson.deserialization.*= # see Jackson's DeserializationFeature
spring.jackson.generator.*= # see Jackson's JsonGenerator.Feature
spring.jackson.mapper.*= # see Jackson's MapperFeature
spring.jackson.parser.*= # see Jackson's JsonParser.Feature
spring.jackson.serialization.*=我想存档相同的代码
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);发布于 2015-05-05 03:33:31
这是SpringBoot1.3.0的增强。
因此,不幸的是,您需要在1.2.3上以编程方式配置它。
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Shop {
//...
}发布于 2016-04-21 00:41:36
将下面的行添加到application.properties文件中。
spring.jackson.default-property-inclusion=non_null
关于2.7之前的杰克逊版本:
spring.jackson.serialization-inclusion=non_null
发布于 2018-01-10 11:16:14
在弃用之前,这是一个很好的解决方案:@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
但现在你应该使用:
@JsonInclude(JsonInclude.Include.NON_NULL) public class ClassName { ...
https://stackoverflow.com/questions/30042507
复制相似问题