SpringBoot 配置统一API对象返回
1、前言
在实际项目开发中,为了便于前端进行响应处理,需要统一返回类格式。特别是在有多个后端开发人员参与的情况下,如果不规范返回类,每个人按照个人习惯返回数据,前端将面临各式各样的返回数据,难以统一处理。为解决这个问题,我们需要规范后端的返回数据,并定义一个统一的返回类,所有数据的返回都由该返回类进行处理。
创建返回类
package com.example.common.response;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 统一API对象返回
* @param <T>
* @Author: TanXJ
* @Date: 2021/10/13 9:31
*/
@Data
@NoArgsConstructor
public class ResultBean<T> {
/** 状态码 */
@ApiModelProperty(value = "状态码", example = "200")
private Integer code;
/** 返回消息 */
@ApiModelProperty(value = "返回消息", example = "操作成功")
private String message;
/** 状态 */
@ApiModelProperty(value = "状态", example = "true")
private boolean status;
/** 返回数据 */
@ApiModelProperty(value = "返回数据", example = "")
private T data;
public ResultBean(Integer code, String message, boolean status, T data) {
this.code = code;
this.message = message;
this.status = status;
this.data = data;
}
public ResultBean(ResultCode resultCode, boolean status, T data) {
this.code = resultCode.getCode();
this.message = resultCode.getMessage();
this.status = status;
this.data = data;
}
public ResultBean(ResultCode resultCode, boolean status) {
this.code = resultCode.getCode();
this.message = resultCode.getMessage();
this.status = status;
this.data = null;
}
public static <T> ResultBean success() {
return new ResultBean<>(ResultCode.OK, true);
}
public static <T> ResultBean message(String message) {
return new ResultBean<>(ResultCode.OK.getCode(), message, true, null);
}
public static <T> ResultBean success(T data) {
return new ResultBean<>(ResultCode.OK, true, data);
}
public static <T> ResultBean fail() {
return new ResultBean<>(ResultCode.ERROR, false);
}
public static <T> ResultBean fail(ResultCode resultCode) {
return new ResultBean<>(resultCode, false);
}
public static <T> ResultBean fail(Integer code, String message) {
return new ResultBean<>(code, message, false, null);
}
public static <T> ResultBean fail(ResultCode resultCode, T data) {
return new ResultBean<>(resultCode, false, data);
}
public static <T> ResultBean fail(Integer code, String message, T data) {
return new ResultBean<>(code, message, false, data);
}
}
创建统一状态码接口
package com.example.common.response;
/**
* 统一状态码接口
*/
public interface IResultCode {
/**
* @return 状态码
*/
Integer getCode();
/**
* @return 返回消息
*/
String getMessage();
}
创建通用状态枚举
package com.example.common.response;
import lombok.Getter;
/**
* 通用状态枚举
*/
@Getter
public enum ResultCode implements IResultCode {
/**
* 成功
*/
OK(200, "成功"),
/**
* 失败
*/
ERROR(500, "失败"),
SYSTEM_ERROR(501, "系统错误"),
UNKNOWN_ERROR(502, "未知错误"),
/* 参数错误:1000~1999 */
PARAM_NOT_VALID(1001, "参数无效"),
PARAM_IS_BLANK(1002, "参数为空"),
PARAM_TYPE_ERROR(1003, "参数类型错误"),
PARAM_NOT_COMPLETE(1004, "参数缺失"),
PARAM_TYPE_REPEAT(1005,"参数重复"),
/* 用户错误 */
USER_NOT_LOGIN(2001, "用户未登录"),
USER_ACCOUNT_EXPIRED(2002, "账号已过期"),
USER_CREDENTIALS_ERROR(2003, "密码错误"),
USER_CREDENTIALS_EXPIRED(2004, "密码过期"),
USER_ACCOUNT_DISABLE(2005, "账号不可用"),
USER_ACCOUNT_LOCKED(2006, "账号被锁定"),
USER_ACCOUNT_NOT_EXIST(2007, "账号不存在"),
USER_ACCOUNT_NOT_NULL(2013, "账号不能为空"),
USER_CREDENTIALS_NOT_NULL(2014, "密码不能为空"),
USER_ACCOUNT_ALREADY_EXIST(2008, "账号已存在"),
USER_ACCOUNT_USE_BY_OTHERS(2009, "账号下线"),
/* token错误 */
TOKEN_IS_NULL(2011, "token不能为空"),
TOKEN_INVALID(2012, "token失效"),
TOKEN_ERROR(2015, "token错误"),
/* 业务错误 */
NO_PERMISSION(3001, "没有权限"),
ADD_FAIL(3002, "新增失败"),
UPDATE_FAIL(3003, "更新失败"),
DELETE_FAIL(3004, "删除失败"),
QUERY_FAIL(3005, "查询失败"),
QUERY_EMPTY(3006, "查询为空"),
QUERY_NOT_EXIST(3007, "查询不存在"),
EMAIL_ERROR(2060, "邮箱格式错误"),
PHONE_ERROR(2061, "电话格式错误"),
/* 文件读写 */
FILE_IS_NULL(2041, "文件为空"),
FILE_WRONG_FORMAT(2042, "文件格式错误"),
FILE_CHUNK_UPLOAD_ERROR(2043, "文件分块上传失败!"),
FILE_CHUNK_MERGE_ERROR(2044, "文件分块合并失败"),
FILE_CONTENT_ERROR(2045, "上传文件内容错误"),
/* 邮箱验证 */
MAILL_ACCOUNT_ERROR(2051, "要重置的邮箱或账号存在错误"),
VERIF_CODE_ERROR(2052,"验证码为空或验证码输入不正确"),
/* 登陆电脑的mac地址验证 */
COMP_MAC_NOT_NULL(2071,"电脑的mac地址不能为空"),
COMP_MAC_ERROR(2072,"登录的电脑mac地址未注册"),
COMP_MAC_FORM_ERROR(2073,"输入的mac地址格式不正确"),
COMP_NAME_NOT_NULL(2074,"未输入电脑名称"),
COMP_MAC_REPEAT(2075,"传入的mac地址已经被注册"),
/** 数据导出 */
EXPORT_FAILURE(2100, "导出失败"),
/** 数据导入 */
IMPORT_FAILURE(2101,"导入失败");
/**
* 返回码
*/
private Integer code;
/**
* 返回消息
*/
private String message;
ResultCode(Integer code, String message) {
this.code = code;
this.message = message;
}
}
代码中使用
@RequestMapping(value = "/test", method = RequestMethod.GET)
public ResultBean test() {
String str = "测试接口成功!";
System.out.println(str);
// return ResultBean.success();
// return ResultBean.success("测试成功");
// return ResultBean.success(str);
return ResultBean.fail(ResultCode.PARAM_IS_BLANK);
// 还有很多种返回方式
}