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;
    }
}

通用返回数据类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;
    }
}

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));
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

令人作呕的溏心蛋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值