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