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));
}
相关推荐
hai405874 分钟前
Spring Boot整合Redis Stack构建本地向量数据库相似性查询
数据库·spring boot·redis
snow@li11 分钟前
vue3 + ts:开发插件 / Plugins / 注册全局实例 / 在 template 与 setup 中使用 / provide、inject
前端·javascript·vue.js
番茄酱料13 分钟前
idea-java项目中的全部接口提取
java·网络安全·intellij-idea
xuanxuanping15 分钟前
Java技术分享
java·开发语言
years_GG18 分钟前
【Git多人开发与协作之团队的环境搭建】
spring boot·深度学习·vue·github·团队开发·个人开发
蜡笔小新..27 分钟前
Java常用类-Object(8个未完待续...)
java·开发语言·object
小钟不想敲代码27 分钟前
第4章 Spring Boot自动配置
java·spring boot·后端
命运之光27 分钟前
【经典】高级动态抽奖系统(HTML,CSS、JS)
前端·css·html
熬夜的猪30 分钟前
现代安全密码哈希算法
java·学习·算法·安全·哈希算法
p-knowledge34 分钟前
外观模式 (Facade Pattern)
java·外观模式