【Jackson】java动态去除返回json中的值
1 业务背景
一般来说,我们后端给前端返回结果的时候,会建一个返回结果类。但这个结果类里的字段并不是所有情况都需要的,例如:分页信息。因此如何在非分页的情况下取消掉分页字段,就是当前我们需要解决的问题。
2 解决方案
我们采用 @JsonInclude 注解对分页信息进行标记。
2.1 @JsonInclude 的用法
在要动态过滤的字段上面,添加以下注释即可(具体规则,根据实际情况进行选择)
@JsonInclude(JsonInclude.Include.NON_NULL)
规则 | 解释 |
---|---|
ALWAYS | 默认值,返回全部字段 |
NON_NULL | 为null的字段不返回 |
NON_EMPTY | 为空或者为 NULL不返回 |
NON_DEFAULT | 为默认值不返回 |
2.2 举例
未动态去除分页信息的旧代码
@Data
@Slf4j
public class JsonVo<T>{
@JsonProperty(value = "code")
//"状态码。200表示成功"
private Integer code;
@JsonProperty(value = "data")
//"结果集"
private List<T> datas;
//"分页信息"
@JsonProperty(value = "pageInfo")
private PageInfo pageInfo = new PageInfo ();;
public static <T> JsonVo ok(String msg){
JsonVoresult = new JsonVo();
result.code = CommonConstants.SUCCESSED_CODE;
result.datas = Collections.singletonList(new SuccessMsgVo(msg)) ;
return result;
}
public static <T> JsonVo<T> ok(List<T> datas){
JsonVo<T> result = new JsonVo<>();
result.code = CommonConstants.SUCCESSED_CODE;
result.datas = datas ;
return result;
}
public static <T> JsonVo<T> ok(IPage<T> data){
JsonVo<T> result = new JsonVo<>();
result.code = CommonConstants.SUCCESSED_CODE;
result.datas = data.getRecords() ;
result.pageInfo.pageNum = Math.toIntExact(data.getCurrent());
result.pageInfo.pageSize = Math.toIntExact(data.getSize());
result.pageInfo.total = data.getTotal();
return result;
}
@Data
public static class PageInfo {
//"页码"
@JsonProperty(value = "pageNumber")
private Integer pageNum = 0;
//"每页返回的数据量"
@JsonProperty(value = "pageSize")
private Integer pageSize = 0;
//"总量"
@JsonProperty(value = "totalCount")
private Long total = 0L;
}
}
修改后的新代码
@Data
@Slf4j
public class JsonVo<T>{
@JsonProperty(value = "code")
//"状态码。200表示成功"
private Integer code;
@JsonProperty(value = "data")
//"结果集"
private List<T> datas;
//"分页信息"
@JsonProperty(value = "pageInfo")
@JsonInclude(JsonInclude.Include.NON_NULL )
private PageInfo pageInfo;
public static <T> JsonVo ok(String msg){
JsonVoresult = new JsonVo();
result.code = CommonConstants.SUCCESSED_CODE;
result.datas = Collections.singletonList(new SuccessMsgVo(msg)) ;
return result;
}
public static <T> JsonVo<T> ok(List<T> datas){
JsonVo<T> result = new JsonVo<>();
result.code = CommonConstants.SUCCESSED_CODE;
result.datas = datas ;
return result;
}
public static <T> JsonVo<T> ok(IPage<T> data){
JsonVo<T> result = new JsonVo<>();
if (result.pageInfo == null){
result.pageInfo = new PageInfo();
}
result.code = CommonConstants.SUCCESSED_CODE;
result.datas = data.getRecords() ;
result.pageInfo.pageNum = Math.toIntExact(data.getCurrent());
result.pageInfo.pageSize = Math.toIntExact(data.getSize());
result.pageInfo.total = data.getTotal();
return result;
}
@Data
public static class PageInfo {
//"页码"
@JsonProperty(value = "pageNumber")
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private Integer pageNum = 0;
//"每页返回的数据量"
@JsonProperty(value = "pageSize")
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private Integer pageSize = 0;
//"总量"
@JsonProperty(value = "totalCount")
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private Long total = 0L;
}
}