1. 项目环境
- IDEA 2020.1.4
- Maven 3.6
- JDK 1.8
- SpringBoot 2.x
项目文件在GitHub(欢迎star⭐):
https://github.com/Gang-bb/Gangbb-SpringBoot
如有疑问或是建议,欢迎评论区留言或者QQ:949526365
2.统一返回对象的目的
可以给前端人员的返回数据更加统一和言简意赅!
如向下面这样:
(成功没有数据)

(成功有数据)

3.处理统一返回对象
3.1 定义错误码和信息的枚举

以后有什么请求错误统一记录在该枚举下。
3.2 定义统一返回对象

import com.gangbb.gangbbspringbootresponse.exception.ApiExceptionEnum;
/**
* @author : Gangbb
* @ClassName : ApiRestResponse
* @Description : 统一返回对象
* @Date : 2021/1/22 22:42
*/
public class ApiRestResponse<T> {
private Integer status;
private String msg;
private T data;
private static final int OK_CODE = 20000;
private static final String OK_MSG = "SUCCESS";
public ApiRestResponse(Integer status, String msg, T data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public ApiRestResponse(Integer status, String msg) {
this.status = status;
this.msg = msg;
}
public ApiRestResponse() {
this(OK_CODE, OK_MSG);
}
/**
* 无返回值返回
* @param <T> 类型
* @return ApiRestResponse对象
*/
public static <T> ApiRestResponse<T> success() {
return new ApiRestResponse<>();
}
/**
* 请求成功带返回值
* @param result 返回值中的data信息
* @param <T> 类型
* @return ApiRestResponse对象
*/
public static <T> ApiRestResponse<T> success(T result) {
ApiRestResponse<T> response = new ApiRestResponse<>();
response.setData(result);
return response;
}
/**
* 失败时返回
* @param code 状态码
* @param msg 错误信息
* @param <T> 类型
* @return ApiRestResponse对象
*/
public static <T> ApiRestResponse<T> error(Integer code, String msg) {
return new ApiRestResponse<>(code, msg);
}
/**
* 通过枚举返回错误信息
* @param exceptionEnum 错误码枚举
* @param <T> 类型
* @return ApiRestResponse对象
*/
public static <T> ApiRestResponse<T> error(ApiExceptionEnum exceptionEnum) {
return new ApiRestResponse<>(exceptionEnum.getCode(), exceptionEnum.getMsg());
}
@Override
public String toString() {
return "ApiRestResponse{" +
"status=" + status +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public static int getOkCode() {
return OK_CODE;
}
public static String getOkMsg() {
return OK_MSG;
}
}
4. 测试使用

