欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • 簡單了解SpringMVC全局異常處理常用方法

    簡單了解SpringMVC全局異常處理常用方法

    這篇文章主要介紹了簡單了解SpringMVC全局異常處理常用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • 簡單易懂的MyBatis分庫分表方案分享

    簡單易懂的MyBatis分庫分表方案分享

    這篇文章主要給大家介紹了關(guān)于MyBatis分庫分表方案的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用MyBatis具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-03-03
  • MyBatis集成Spring流程詳解

    MyBatis集成Spring流程詳解

    在實際開發(fā)中不僅僅是要展示數(shù)據(jù),還要構(gòu)成數(shù)據(jù)模型添加數(shù)據(jù),這篇文章主要介紹了SpringBoot集成Mybatis操作數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • Spring Boot Security配置教程

    Spring Boot Security配置教程

    在本文里我們給大家分享了關(guān)于Spring Boot Security配置的相關(guān)步驟以及注意要點,需要的朋友們跟著操作下。
    2019-05-05
  • 最新評論