1、描述
@JsonInclude(JsonInclude.Include.NON_NULL)标记是jackson包提供的json序列化方法,已经集成于Springboot2.0中,此方法的配置意在可以对实体json序列化的时候进行对应的数值处理。
2、使用
- 用注解的方式放在标记类或者属性
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User implements Serializable {
private String username;
private String password;
@JsonInclude(JsonInclude.Include.NON_NULL)
private String token;
}
- 用配置文件的方式全局配置(可能会影响到其他某些服务接口),例SpringBoot的application.yml
spring:
jackson:
default-property-inclusion: non_null
3、示例
- 使用前:{"username":"admin", "password":"admin123", "token":null}
- 使用后:{"username":"admin", "password":"admin123"}