对于 Spring Boot 1.2.3,如何在 JSON 序列化中设置忽略空值? [英] For Spring Boot 1.2.3, how to set ignore null value in JSON serialization?

查看:66
本文介绍了对于 Spring Boot 1.2.3,如何在 JSON 序列化中设置忽略空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Spring Boot 1.2.3 中,我们可以通过属性文件自定义 Jackson ObjectMapper.但是我没有找到一个属性可以在将 Object 序列化为 JSON 字符串时设置 Jackson 忽略空值.

In the Spring Boot 1.2.3, we can customize the Jackson ObjectMapper via properties file. But I didn't find a attribute can set Jackson ignore null value when serialization the Object to JSON string.

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);

推荐答案

这是 Spring Boot 1.3.0 的增强功能.

This was an enhancement for Spring Boot 1.3.0.

很遗憾,您需要在 1.2.3 上以编程方式对其进行配置

So unfortunately you'll need to configure it programmatically on 1.2.3

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class Shop {
    //...
}

这篇关于对于 Spring Boot 1.2.3,如何在 JSON 序列化中设置忽略空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆