Jackson java動態(tài)去除返回json中的值方式
更新時間:2024年12月17日 08:41:54 作者:愛寫B(tài)UG的老冉
文章介紹了在Java中使用@JsonInclude注解動態(tài)去除返回JSON中的非必需字段(如分頁信息)的解決方案,通過在字段上添加@JsonInclude注解并選擇合適的策略(如NON_NULL或NON_EMPTY),可以在非分頁情況下取消分頁字段,從而提高返回結(jié)果的靈活性和效率
【Jackson】java動態(tài)去除返回json中的值
1 業(yè)務(wù)背景
一般來說,我們后端給前端返回結(jié)果的時候,會建一個返回結(jié)果類。
但這個結(jié)果類里的字段并不是所有情況都需要的,例如:分頁信息。
因此如何在非分頁的情況下取消掉分頁字段,就是當前我們需要解決的問題。
2 解決方案
我們采用 @JsonInclude 注解對分頁信息進行標記。
2.1 @JsonInclude 的用法
在要動態(tài)過濾的字段上面,添加以下注釋即可(具體規(guī)則,根據(jù)實際情況進行選擇)
@JsonInclude(JsonInclude.Include.NON_NULL)
規(guī)則 | 解釋 |
---|---|
ALWAYS | 默認值,返回全部字段 |
NON_NULL | 為null的字段不返回 |
NON_EMPTY | 為空或者為 NULL不返回 |
NON_DEFAULT | 為默認值不返回 |
2.2 舉例
未動態(tài)去除分頁信息的舊代碼
@Data @Slf4j public class JsonVo<T>{ @JsonProperty(value = "code") //"狀態(tài)碼。200表示成功" private Integer code; @JsonProperty(value = "data") //"結(jié)果集" 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; //"每頁返回的數(shù)據(jù)量" @JsonProperty(value = "pageSize") private Integer pageSize = 0; //"總量" @JsonProperty(value = "totalCount") private Long total = 0L; } }
修改后的新代碼
@Data @Slf4j public class JsonVo<T>{ @JsonProperty(value = "code") //"狀態(tài)碼。200表示成功" private Integer code; @JsonProperty(value = "data") //"結(jié)果集" 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; //"每頁返回的數(shù)據(jù)量" @JsonProperty(value = "pageSize") @JsonInclude(JsonInclude.Include.NON_DEFAULT) private Integer pageSize = 0; //"總量" @JsonProperty(value = "totalCount") @JsonInclude(JsonInclude.Include.NON_DEFAULT) private Long total = 0L; } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
【Redis緩存機制】詳解Java連接Redis_Jedis_事務(wù)
這篇文章主要介紹了【Redis緩存機制】詳解Java連接Redis_Jedis_事務(wù),詳細的介紹了Jedis事務(wù)和實例,有興趣的可以了解一下。2016-12-12