菏澤公司做網(wǎng)站深圳seo優(yōu)化培訓
背景
我們在寫接口的時候一般不會直接返回給前端數(shù)據(jù),而是會有響應(yīng)體,比如 code、data、msg,這樣就有一個統(tǒng)一的結(jié)構(gòu)方便前端處理,那么今天就來封裝一個統(tǒng)一的響應(yīng)體
封裝基本響應(yīng)體
1、在 config 包里新建 ApiResponse.java
package com.zhangyu.config;import lombok.Getter;
import lombok.Setter;public class ApiResponse<T> {@Getter@Setterprivate int code;@Getter@Setterprivate T data;@Getter@Setterprivate String msg;public ApiResponse(int code, T data, String msg) {this.code = code;this.data = data;this.msg = msg;}public static <T> ApiResponse<T> success (T data) {return new ApiResponse<>(200, data, "成功");}public static <T> ApiResponse<T> fail (int code, String msg) {return new ApiResponse<>(code, null, msg);}
}
2、在控制器中使用
package com.zhangyu.controller;import com.zhangyu.config.ApiResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
@RequestMapping("/users")
public class UserController {@GetMapping("success")public ApiResponse<Object> getSuccessTest() {JsonObject jsonObject = JsonObject.create("a", 1);return ApiResponse.success(jsonObject);}@GetMapping("fail")public ApiResponse<Object> getFailTest () {return ApiResponse.fail(404, "請求失敗");}
}
這是一個非常簡單的封裝,如果更復雜的可以在這個基礎(chǔ)上增加
封裝分頁
封裝分頁我見過兩種形式的,如下:
{"code": 200,"data": {"list": [{"id": 1,"name": "張三","email": "865091936@qq.com"},{"id": 2,"name": "劉能","email": "xxx@xx.com"}],"pageNum": 1,"total": 2,"totalPage": 1,"pageSize": 10},"msg": "成功"
}
{"code": 200,"data": [{"id": 1,"name": "張三","email": "865091936@qq.com"},{"id": 2,"name": "劉能","email": "xxx@xx.com"}],"paging": {// 第幾頁"pageNum": 1,// 總數(shù)"total": 2,// 多少頁"totalPage": 1,// 每頁多少條"pageSize": 10},"msg": "成功"
}
這里以第二種為例
1、封裝
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;import java.util.List;/*** 通用分頁數(shù)據(jù)封裝類*/
public class CommonPage<T> {/*** 當前頁碼*/private Integer pageNum;/*** 每頁數(shù)量*/private Integer pageSize;/*** 總頁數(shù)*/private Integer totalPage;/*** 總條數(shù)*/private Long total;/*** 分頁數(shù)據(jù)*/private List<T> list;/*** 將PageHelper分頁后的list轉(zhuǎn)為分頁信息*/public static <T> CommonPage<T> restPage(List<T> list) {CommonPage<T> result = new CommonPage<T>();PageInfo<T> pageInfo = new PageInfo<T>(list);result.setTotalPage(pageInfo.getPages());result.setPageNum(pageInfo.getPageNum());result.setPageSize(pageInfo.getPageSize());result.setTotal(pageInfo.getTotal());result.setList(pageInfo.getList());return result;}/*** 將SpringData分頁后的list轉(zhuǎn)為分頁信息*/public static <T> CommonPage<T> restPage(Page<T> pageInfo) {CommonPage<T> result = new CommonPage<T>();result.setTotalPage(pageInfo.getTotalPages());result.setPageNum(pageInfo.getNumber());result.setPageSize(pageInfo.getSize());result.setTotal(pageInfo.getTotalElements());result.setList(pageInfo.getContent());return result;}// getter and setter
}
2、使用
public ApiResponse<Object> getAllUsersForMybatis() {// 使用PageHelper進行分頁,第1頁每頁5條PageHelper.startPage(1, 5);// 獲取某個數(shù)據(jù)的listList<UserForMybatis> userList = userMapper.findAll();// 獲取pageInfo,也就是上面的封裝PageInfo pageInfo = new PageInfo(userList);// 返回最終數(shù)據(jù)結(jié)構(gòu)return ApiResponse.success(CommonPage.restPage(userList));
}
users 表里有 6 條數(shù)據(jù),這里使用PageHelper進行分頁,第1頁每頁5條,所以取了前五條