Spring Boot定义类处理API通用返回数据

枚举类AppHttpCodeEnum

java 复制代码
package com.yutu.common.core.constant;

public enum AppHttpCodeEnum {


    SUCCESS(200, "操作成功"), // 成功段0

    REQUEST_NOT_FOUND(404, "请求不存在!"),
    HTTP_BAD_METHOD(405, "请求方式不支持!"),
    BAD_REQUEST(400, "请求异常!"),
    PARAM_NOT_MATCH(400, "参数不匹配!"),
    PARAM_NOT_NULL(400, "参数不能为空!"),
    UNAUTHORIZED(401, "请先登录!"),
    ACCESS_DENIED(403, "权限不足!"),
    PARAM_REQUIRE(500, "缺少参数"),    // 参数错误 500~1000
    PARAM_INVALID(501, "无效参数"),
    PARAM_IMAGE_FORMAT_ERROR(502, "图片格式有误"),
    SERVER_ERROR(503, "服务器内部错误"),
    REQUEST_TIMEOUT(508, "服务请求超时,稍后重试"),

    //用户接口枚举
    USER_INSERT_USERNAME_EXISTS(1001, "新增失败,该用户名已存在"),
    USER_INSERT_PHONE_EXISTS(1002, "新增失败,该电话号码已存在"),
    USER_INSERT_EMAIL_EXISTS(1003, "新增失败,该电话邮箱已存在"),
    USER_UPDATE_PHONE_EXISTS(1004, "修改失败,该电话号码已存在"),
    USER_UPDATE_EMAIL_EXISTS(1005, "修改失败,该电话邮箱已存在"),
    USER_NOT_UPDATE_ADMIN(1006, "修改失败,无法修改超级管理员信息"),
    USER_GET_ERROR(1007, "获取用户失败"),
    USERNAME_PASSWORD_ERROR(1008, "用户名或密码错误!"),
    USER_DISABLED(1009, "当前用户已被锁定,请联系管理员解锁!"),

    //岗位接口枚举
    POST_INSERT_NAME_EXISTS(1501, "新增失败,该岗位名称已存在"),
    POST_INSERT_CODE_EXISTS(1502, "新增失败,该岗位编码已存在"),
    POST_UPDATE_NAME_EXISTS(1501, "修改失败,该岗位名称已存在"),
    POST_UPDATE_CODE_EXISTS(1502, "修改失败,该岗位编码已存在"),
    //菜单接口枚举
    MENU_INSERT_NAME_EXISTS(2001, "新增失败,该菜单名称已存在"),
    MENU_UPDATE_NAME_EXISTS(2002, "修改失败,该菜单名称已存在"),
    MENU_UPDATE_PARENT_IS_SELF(2003, "修改失败,父级不能是自己"),
    MENU_DELETE_HAS_CHILD(2004, "删除失败,该菜单存在子菜单"),
    MENU_DELETE_DIST(2005, "删除失败,菜单已分配"),

    //角色接口枚举
    ROLE_INSERT_NAME_EXISTS(2501, "新增失败,该角色名称已存在"),
    ROLE_INSERT_KEY_EXISTS(2502, "新增失败,该角色权限已存在"),
    ROLE_UPDATE_NAME_EXISTS(2503, "修改失败,该角色名称已存在"),
    ROLE_UPDATE_KEY_EXISTS(2504, "修改失败,该角色权限已存在"),
    ROLE_UPDATE_ERROR(2505, "修改角色失败,联系管理员"),
    ROLE_NOT_UPDATE_ADMIN(2506, "修改失败,无法修改超级管理员角色信息"),
    ROLE_DELETE_DIST(2507, "删除失败,该角色已经分配"),
    //部门接口枚举
    DEPT_INSERT_NAME_EXISTS(3001, "新增部门失败,该部门下此部门名称已存在"),
    DEPT_UPDATE_NAME_EXISTS(3001, "修改部门失败,该部门下此部门名称已存在"),
    DEPT_UPDATE_PARENT_IS_SELF(3001, "修改失败,父级不能是自己"),
    DEPT_UPDATE_CHILD_HAS_ENABLE(3001, "修改部门状态失败,子部门存着未停用状态"),
    DEPT_DELETE_HAS_CHILD(3001, "删除失败,该部门存在子级部门"),
    DEPT_DELETE_HAS_USER(3002, "删除失败,该部门存在用户"),
    //字典类型接口枚举
    DICTTYPE_INSERT_TYPE_EXISTS(4001, "新增失败,该字典类型已存在"),
    DICTTYPE_UPDATE_TYPE_EXISTS(4002, "修改失败,该字典类型已存在"),


    //权限认证枚举
    TOKEN_EXPIRED(5002, "token 已过期,请重新登录!"),
    TOKEN_PARSE_ERROR(5002, "token 解析失败,请尝试重新登录!"),
    TOKEN_USER_ERROR(5002, "token 解析用户信息失败,请尝试重新登录!"),

    //公园枚举
    PARK_INSERT_ERROR(6001, "公园新增失败,联系管理员解决问题"),
    PARK_UPDATE_ERROR(6002, "公园修改失败,联系管理员解决问题"),
    PARK_DELETE_ERROR(6003, "公园删除失败,联系管理员解决问题"),
    PARK_INSERT_EXISTS(6004, "公园新增失败,该公园名称已存在"),
    PARK_UPDATE_EXISTS(6004, "公园修改失败,该公园名称已存在"),

    ;
    int code;
    String errorMessage;

    AppHttpCodeEnum(int code, String errorMessage) {
        this.code = code;
        this.errorMessage = errorMessage;
    }

    public int getCode() {
        return code;
    }

    public String getErrorMessage() {
        return errorMessage;
    }
}

通用返回数据类ResponseResult

java 复制代码
package com.yutu.common.core.web.domain;


import com.yutu.common.core.constant.AppHttpCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 通用的结果返回类
 *
 * @param <T>
 */
@ApiModel("api通用返回数据")
public class ResponseResult<T> implements Serializable {

    private String host;

    @ApiModelProperty("标识代码,0为成功,非0表示失败")
    private Integer code;
    @ApiModelProperty("提示信息,供报错使用")
    private String errorMessage;
    @ApiModelProperty("提示信息,供正确使用")
    private String message;
    @ApiModelProperty("返回数据")
    private T data;


    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public ResponseResult() {
        this.code = 200;
    }

    public ResponseResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg, T data) {
        this.code = code;
        this.errorMessage = msg;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.errorMessage = msg;
    }
    public static <T> ResponseResult<T> errorMsg(Integer code, String msg) {
        ResponseResult<T> result = new ResponseResult<T>();
        return result.error(code, msg);
    }
    public static <T> ResponseResult<T> errorMsg(String msg) {
        ResponseResult<T> result = new ResponseResult<T>();
        return result.error(500, msg);
    }

    public static <T> ResponseResult<T> errorResult(T data) {
        ResponseResult<T> result = new ResponseResult<T>();
        return result.error(data);
    }

    public static <T> ResponseResult<T> okResult(int code, String msg) {
        ResponseResult<T> result = new ResponseResult<T>();
        return result.ok(code, null, msg);
    }

    public static <T> ResponseResult<T> okResult() {
        return setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }

    public static <T> ResponseResult<T> okResult(T data) {
        ResponseResult<T> result = setAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        if (data != null) {
            result.setData(data);
        }
        return result;
    }


    public static <T> ResponseResult<T> errorResult(AppHttpCodeEnum enums) {
        return setAppHttpCodeEnum(enums, enums.getErrorMessage());
    }

    public static <T> ResponseResult<T> errorResult(AppHttpCodeEnum enums, String errorMessage) {
        return setAppHttpCodeEnum(enums, errorMessage);
    }

    public static <T> ResponseResult<T> setAppHttpCodeEnum(AppHttpCodeEnum enums) {
        return okResult(enums.getCode(), enums.getErrorMessage());
    }

    private static <T> ResponseResult<T> setAppHttpCodeEnum(AppHttpCodeEnum enums, String errorMessage) {
        return okResult(enums.getCode(), errorMessage);
    }

    public ResponseResult<T> error(Integer code, String msg) {
        this.code = code;
        this.errorMessage = msg;
        return this;
    }

    public ResponseResult<T> error(T data) {
        this.code = 500;
        this.data = data;
        return this;
    }

    public ResponseResult<T> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }

    public ResponseResult<T> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.message = msg;
        return this;
    }

    public ResponseResult<T> ok(T data) {
        this.data = data;
        return this;
    }

    public ResponseResult<T> put(String key, Object value) {

        return this;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

Controller中使用

java 复制代码
@Log(title = "养护计划", operate = "获取计划流程", businessType = BusinessType.QUERY)
@GetMapping("/getCuringPlanProcessInfoList")
@ApiOperationSupport(order = 1, author = "ckm")
@ApiOperation(value = "获取计划流程", tags = "养护计划接口")
public ResponseResult<List<CuringPlanProcessInfo>> getCuringPlanProcessInfoList(@RequestParam(value = "id")Integer id){
	if(ObjectUtils.isEmpty(id)){
		return ResponseResult.errorMsg(501,"必须有计划ID");
	}

	return ResponseResult.okResult(curingPlanInfoService.getCuringPlanProcessInfoList(id));
}
相关推荐
blammmp6 分钟前
Java:数据结构-枚举
java·开发语言·数据结构
bysking16 分钟前
【前端-组件】定义行分组的表格表单实现-bysking
前端·react.js
暗黑起源喵24 分钟前
设计模式-工厂设计模式
java·开发语言·设计模式
WaaTong29 分钟前
Java反射
java·开发语言·反射
王哲晓31 分钟前
第三十章 章节练习商品列表组件封装
前端·javascript·vue.js
fg_41134 分钟前
无网络安装ionic和运行
前端·npm
理想不理想v36 分钟前
‌Vue 3相比Vue 2的主要改进‌?
前端·javascript·vue.js·面试
酷酷的阿云1 小时前
不用ECharts!从0到1徒手撸一个Vue3柱状图
前端·javascript·vue.js
微信:137971205871 小时前
web端手机录音
前端
齐 飞1 小时前
MongoDB笔记01-概念与安装
前端·数据库·笔记·后端·mongodb