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));
}
相关推荐
是小崔啊1 分钟前
Spring源码05 - AOP深入代理的创建
java·spring
milk_yan7 分钟前
MinIO的安装与使用
linux·数据仓库·spring boot
等一场春雨30 分钟前
Java设计模式 八 适配器模式 (Adapter Pattern)
java·设计模式·适配器模式
索然无味io42 分钟前
XML外部实体注入--漏洞利用
xml·前端·笔记·学习·web安全·网络安全·php
一弓虽1 小时前
java基础学习——jdbc基础知识详细介绍
java·学习·jdbc·连接池
王磊鑫1 小时前
Java入门笔记(1)
java·开发语言·笔记
ThomasChan1231 小时前
Typescript 多个泛型参数详细解读
前端·javascript·vue.js·typescript·vue·reactjs·js
爱学习的狮王1 小时前
ubuntu18.04安装nvm管理本机node和npm
前端·npm·node.js·nvm
硬件人某某某1 小时前
Java基于SSM框架的社区团购系统小程序设计与实现(附源码,文档,部署)
java·开发语言·社区团购小程序·团购小程序·java社区团购小程序
东锋1.31 小时前
使用 F12 查看 Network 及数据格式
前端