1. 导包
<dependency><groupId>com.fasterxml.jackson.module</groupId><artifactId>jackson-module-kotlin</artifactId>
</dependency>
2. 类上加注解
@JsonInclude(value= JsonInclude.Include.NON_NULL)
3. 示例
@Data
@JsonInclude(value= JsonInclude.Include.NON_NULL)
public class ResultVo {private String name;private String age;
}
- 如果name和age都为null,则返回值为:
{}
- 如果name为null,age不为null,则返回值为:
{"age":"23"}
- 如果name不为null,age不为null,则返回值为:
{"name":"111","age":"23"}
4. 如果不加这个注解,为null的显示效果为:
{"name":null,"age":null}
{"name":null,"age":"23"}