背景
我们在写接口的时候一般不会直接返回给前端数据,而是会有响应体,比如 code、data、msg,这样就有一个统一的结构方便前端处理,那么今天就来封装一个统一的响应体
封装基本响应体
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, "请求失败");}
}
这是一个非常简单的封装,如果更复杂的可以在这个基础上增加
封装分页
封装分页我见过两种形式的,如下:
{"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,// 总数"total": 2,// 多少页"totalPage": 1,// 每页多少条"pageSize": 10},"msg": "成功"
}
这里以第二种为例
1、封装
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;import java.util.List;/*** 通用分页数据封装类*/
public class CommonPage<T> {/*** 当前页码*/private Integer pageNum;/*** 每页数量*/private Integer pageSize;/*** 总页数*/private Integer totalPage;/*** 总条数*/private Long total;/*** 分页数据*/private List<T> list;/*** 将PageHelper分页后的list转为分页信息*/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转为分页信息*/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);// 获取某个数据的listList<UserForMybatis> userList = userMapper.findAll();// 获取pageInfo,也就是上面的封装PageInfo pageInfo = new PageInfo(userList);// 返回最终数据结构return ApiResponse.success(CommonPage.restPage(userList));
}
users 表里有 6 条数据,这里使用PageHelper进行分页,第1页每页5条,所以取了前五条