当前位置:   article > 正文

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

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

枚举类AppHttpCodeEnum

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92

通用返回数据类ResponseResult

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171

Controller中使用

@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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/439093
推荐阅读
相关标签
  

闽ICP备14008679号