Spring自带Jackson序列化json去除值为null的字段

1. @JsonInclude

在类或者字段上加注解@JsonInclude(JsonInclude.Include.NON_NULL),Spring请求返回对象时就不会包含值为null的字段

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BaseQuery {

    private Integer uid;
    private Integer page;
    private String num;
}

2. ObjectMapper

可以使用ObjectMapper.writeValueAsString()来处理

ObjectMapper objectMapper = new ObjectMapper();
String value = objectMapper.writeValueAsString(query);
System.out.println(value);
posted @ 2021-01-29 10:06  林宇风  阅读(1984)  评论(0编辑  收藏  举报