java统一响应信息主体(工具类)

2 篇文章 0 订阅
package com.sqx.config;

import lombok.Data;
import org.springframework.context.i18n.LocaleContextHolder;

import java.io.Serializable;
import java.util.Locale;

import static com.sqx.config.ResponseCode.SYS_SUCCESSFUL_REQUEST;

/**
 * 统一响应信息主体
 *
 * @author zc
 */
@Data
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private T data;
    private Integer code;
    private String message;

    public ApiResult() {
        this(null, ResponseCode.instance(SYS_SUCCESSFUL_REQUEST));
    }

    public ApiResult(T data) {
        this(data, ResponseCode.instance(SYS_SUCCESSFUL_REQUEST));
    }

    public ApiResult(ResponseCode responseCode) {
        this(null, responseCode);
    }

    public ApiResult(ResponseCode responseCode, String chMsg, String enMsg) {
        this(null, responseCode, chMsg, enMsg);
    }

    public ApiResult(T data, ResponseCode responseCode) {
        this(data, responseCode, null, null);
    }

    public ApiResult(T data, ResponseCode responseCode, String chMsg, String enMsg) {
        Locale locale = LocaleContextHolder.getLocale();
        if (Locale.CHINESE.getLanguage().equals(locale.getLanguage())) {
            this.message = (chMsg == null) ? responseCode.getChMsg() : chMsg;
        } else {
            this.message = (enMsg == null) ? responseCode.getEnMsg() : enMsg;
        }
        this.data = data;
        this.code = responseCode.getCode();
    }

    public static <T> ApiResult<T> ok() {
        return new ApiResult<>();
    }

    public static <T> ApiResult<T> ok(T data) {
        return new ApiResult<>(data);
    }
}
package com.sqx.config;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回信息主体
 *
 * @author zc
 */
@Data
public class ResponseCode implements Serializable {
    private static final long serialVersionUID = 1L;
    private static final Map<String, ResponseCode> MAP = new HashMap<>();

    /* *****************************  200 请求成功  **************************** */

    /* -------------  00 系统状态  ------------- */
    /**
     * 请求成功
     */
    public static final String SYS_SUCCESSFUL_REQUEST = "SYS_SUCCESSFUL_REQUEST";

    /* -------------  01 通用状态  ------------- */
    /**
     * 不存在该记录
     */
    public static final String COMMON_NOT_EXIST = "COMMON_NOT_EXIST";
    /**
     * 已存在该记录
     */
    public static final String COMMON_ALREADY_EXIST = "COMMON_ALREADY_EXIST";
    /**
     * 不许重复执行
     */
    public static final String COMMON_NOT_ALLOW_REPEAT = "COMMON_NOT_ALLOW_REPEAT";
    /**
     * 不允许的操作
     */
    public static final String COMMON_NOT_ALLOW = "COMMON_NOT_ALLOW";
    /**
     * 记录已被修改
     */
    public static final String COMMON_CHANGED = "COMMON_CHANGED";
    /**
     * 请求失败
     */
    public static final String COMMON_FAIL_REQUEST = "COMMON_FAIL_REQUEST";
    /**
     * 不支持此操作
     */
    public static final String COMMON_NOT_SUPPORT = "COMMON_NOT_SUPPORT";
    /**
     * 手机号已经注册
     */
    public static final String MOBILE_NUMBER_REGISTERED = "MOBILE_NUMBER_REGISTERED";
    /**
     * 邀请码无效
     */
    public static final String INVALID_INVITATION_CODE = "INVALID_INVITATION_CODE";

    /* -------------  02 授权状态  ------------- */
    /**
     * 授权失败
     */
    public static final String PERMISSION_AUTHORIZE_FAILED = "PERMISSION_AUTHORIZE_FAILED";

    /* -------------  03 角色状态  ------------- */
    /**
     * 无效角色
     */
    public static final String ROLE_INVALID = "ROLE_INVALID";
    /**
     * 获取角色失败
     */
    public static final String ROLE_NOT_FOUND = "ROLE_NOT_FOUND";

    /* *****************************  400 请求错误  **************************** */

    /* -------------  00 系统状态  ------------- */
    /**
     * 请求body格式错误
     */
    public static final String SYS_BODY_FORMAT_ERROR = "SYS_BODY_FORMAT_ERROR";
    /**
     * 上传文件大小超过限制
     */
    public static final String SYS_FILE_SIZE_OUT_OF_GAUGE = "SYS_FILE_SIZE_OUT_OF_GAUGE";
    /**
     * 不支持的Content
     */
    public static final String SYS_UNSUPPORTED_CONTENT_TYPE = "SYS_UNSUPPORTED_CONTENT_TYPE";
    /**
     * 丢失请求参数
     */
    public static final String COMMON_MISS_PARAMETER = "COMMON_MISS_PARAMETER";
    /**
     * 无效的参数
     */
    public static final String COMMON_INVALID_PARAM = "COMMON_INVALID_PARAM";

    /* -------------  01 通用状态  ------------- */
    /**
     * 参数校验错误
     */
    public static final String COMMON_VALIDATE_ERROR = "COMMON_VALIDATE_ERROR";
    /**
     * 不在允许值范围内
     */
    public static final String COMMON_NOT_IN_ALLOWED_SCOPE = "COMMON_NOT_IN_ALLOWED_SCOPE";
    /**
     * 验证码错误
     */
    public static final String VERIFICATION_CODE_ERROR = "VERIFICATION_CODE_ERROR";
    /**
     * 验证码已过期
     */
    public static final String VERIFICATION_CODE_EXPIRED = "VERIFICATION_CODE_EXPIRED";

    /* -------------  02 授权状态  ------------- */
    /**
     * 用户名或密码错误
     */
    public static final String PERMISSION_INVALID_USERNAME_PASSWORD = "PERMISSION_INVALID_USERNAME_PASSWORD";

    /* -------------  03 角色状态  ------------- */
    /**
     * 角色不匹配
     */
    public static final String ROLE_NOT_MATCHED = "ROLE_NOT_MATCHED";

    /* *****************************  401 未授权  **************************** */
    /* -------------  02 授权状态  ------------- */
    /**
     * 未授权
     */
    public static final String PERMISSION_UNAUTHORIZED = "PERMISSION_UNAUTHORIZED";
    /**
     * IP受限
     */
    public static final String PERMISSION_IP_RESTRICTION = "PERMISSION_IP_RESTRICTION";
    /**
     * 权限不足
     */
    public static final String PERMISSION_DENIED = "PERMISSION_DENIED";
    /**
     * 账号失效
     */
    public static final String PERMISSION_INVALID_ACCOUNT = "PERMISSION_INVALID_ACCOUNT";

    /* *****************************  404 路由错误  **************************** */
    /* -------------  00 系统状态  ------------- */
    /**
     * 路由不存在
     */
    public static final String SYS_NOT_FOUND = "SYS_NOT_FOUND";

    /* *****************************  405 不支持的请求方法  **************************** */
    /* -------------  00 系统状态  ------------- */
    /**
     * 不支持的请求方法
     */
    public static final String SYS_UNSUPPORTED_METHOD = "SYS_UNSUPPORTED_METHOD";

    /* *****************************  500 系统内部错误  **************************** */
    /* -------------  00 系统状态  ------------- */
    /**
     * 系统繁忙
     */
    public static final String SYS_INTERNAL_SERVER_ERROR = "SYS_INTERNAL_SERVER_ERROR";


    static {
        /* *****************************  200 请求成功  **************************** */
        MAP.put(SYS_SUCCESSFUL_REQUEST, new ResponseCode(HttpStatus.OK, 2000000, "请求成功", "Request successful"));
        MAP.put(COMMON_NOT_EXIST, new ResponseCode(HttpStatus.OK, 2000100, "记录不存在", "Record does not exist"));
        MAP.put(COMMON_ALREADY_EXIST, new ResponseCode(HttpStatus.OK, 2000101, "记录已存在", "Record already exist"));
        MAP.put(COMMON_NOT_ALLOW_REPEAT, new ResponseCode(HttpStatus.OK, 2000101, "不许重复执行", "Duplicate execution not allowed"));
        MAP.put(COMMON_NOT_ALLOW, new ResponseCode(HttpStatus.OK, 2000103, "不允许的操作", "Operation not allowed"));
        MAP.put(COMMON_CHANGED, new ResponseCode(HttpStatus.OK, 2000104, "记录已被修改", "Record has been modified"));
        MAP.put(COMMON_FAIL_REQUEST, new ResponseCode(HttpStatus.OK, 2000105, "请求失败", "Request was aborted"));
        MAP.put(COMMON_NOT_SUPPORT, new ResponseCode(HttpStatus.OK, 2000106, "不支持此操作", "Unsupported operation"));
        MAP.put(MOBILE_NUMBER_REGISTERED, new ResponseCode(HttpStatus.OK, 2000107, "手机号已经注册", "Mobile number registered"));
        MAP.put(INVALID_INVITATION_CODE, new ResponseCode(HttpStatus.OK, 2000108, "邀请码无效", "Invalid invitation code"));
        MAP.put(PERMISSION_AUTHORIZE_FAILED, new ResponseCode(HttpStatus.OK, 2000200, "授权失败", "Authorization failed"));
        MAP.put(ROLE_INVALID, new ResponseCode(HttpStatus.OK, 2000300, "无效角色", "Invalid role"));
        MAP.put(ROLE_NOT_FOUND, new ResponseCode(HttpStatus.OK, 2000301, "获取角色失败", "Failed to get role"));
        /* *****************************  400 请求错误  **************************** */
        MAP.put(SYS_BODY_FORMAT_ERROR, new ResponseCode(HttpStatus.BAD_REQUEST, 4000000, "请求body格式错误", "Request body format error"));
        MAP.put(SYS_FILE_SIZE_OUT_OF_GAUGE, new ResponseCode(HttpStatus.BAD_REQUEST, 4000001, "上传文件大小超过限制", "Upload file size exceeds limit"));
        MAP.put(SYS_UNSUPPORTED_CONTENT_TYPE, new ResponseCode(HttpStatus.BAD_REQUEST, 4000002, "不支持的Content-Type", "Unsupported Content-Type"));
        MAP.put(COMMON_MISS_PARAMETER, new ResponseCode(HttpStatus.BAD_REQUEST, 4000003, "丢失请求参数", "Missing request parameters"));
        MAP.put(COMMON_INVALID_PARAM, new ResponseCode(HttpStatus.BAD_REQUEST, 4000004, "无效的参数", "Invalid parameter"));
        MAP.put(COMMON_VALIDATE_ERROR, new ResponseCode(HttpStatus.BAD_REQUEST, 4000100, "未通过参数校验", "Failed parameter verification"));
        MAP.put(COMMON_NOT_IN_ALLOWED_SCOPE, new ResponseCode(HttpStatus.BAD_REQUEST, 4000101, "不在允许值范围内", "Not in allowed scope"));
        MAP.put(VERIFICATION_CODE_ERROR, new ResponseCode(HttpStatus.BAD_REQUEST, 4000102, "验证码错误", "Verification code error"));
        MAP.put(VERIFICATION_CODE_EXPIRED, new ResponseCode(HttpStatus.BAD_REQUEST, 4000103, "验证码已过期", "Verification code expired"));
        MAP.put(PERMISSION_INVALID_USERNAME_PASSWORD, new ResponseCode(HttpStatus.BAD_REQUEST, 4000200, "用户名或密码错误", "Wrong user name or password"));
        MAP.put(ROLE_NOT_MATCHED, new ResponseCode(HttpStatus.BAD_REQUEST, 4000300, "角色不匹配", "Role mismatch"));
        /* *****************************  401 未授权  **************************** */
        MAP.put(PERMISSION_UNAUTHORIZED, new ResponseCode(HttpStatus.UNAUTHORIZED, 4010200, "未授权", "Unauthorized"));
        MAP.put(PERMISSION_IP_RESTRICTION, new ResponseCode(HttpStatus.UNAUTHORIZED, 4010201, "IP受限", "IP restricted"));
        MAP.put(PERMISSION_DENIED, new ResponseCode(HttpStatus.UNAUTHORIZED, 4010202, "权限不足", "Insufficient authority"));
        MAP.put(PERMISSION_INVALID_ACCOUNT, new ResponseCode(HttpStatus.UNAUTHORIZED, 4010203, "无效账户", "无效账户"));
        /* *****************************  404 路由错误  **************************** */
        MAP.put(SYS_NOT_FOUND, new ResponseCode(HttpStatus.NOT_FOUND, 4040000, "路由不存在", "Route does not exist"));
        /* *****************************  405 不支持的请求方法  **************************** */
        MAP.put(SYS_UNSUPPORTED_METHOD, new ResponseCode(HttpStatus.METHOD_NOT_ALLOWED, 4050000, "不支持的请求方法", "Unsupported request method"));
        /* *****************************  500 系统内部错误  **************************** */
        MAP.put(SYS_INTERNAL_SERVER_ERROR, new ResponseCode(HttpStatus.INTERNAL_SERVER_ERROR, 5000000, "系统繁忙", "System error"));
    }

    /**
     * http状态码
     */
    private HttpStatus status;
    /**
     * 自定义状态码
     * 自定义状态码一共5位,前3位取http状态码,中间2位表示状态类型,后两位表示具体状态码
     * 状态类型(每个人根据自己需求进行追加)
     * + 00:系统状态
     * + 01:通用状态
     * + 02:授权状态
     * + 03:角色状态
     */
    private Integer code;
    /**
     * 中文message
     */
    private String chMsg;
    /**
     * 英文message
     */
    private String enMsg;
    /**
     * 字段名
     */
    private String field;

    private ResponseCode(HttpStatus status, Integer code, String chMsg, String enMsg) {
        this.status = status;
        this.code = code;
        this.chMsg = chMsg;
        this.enMsg = enMsg;
    }

    @SuppressWarnings("unused")
    public ResponseCode() {
    }

    public static ResponseCode defaultValidate(String msg) {
        ResponseCode instance = instance(msg);
        if (instance == null) {
            instance = instance(COMMON_VALIDATE_ERROR);
            if (msg != null && !msg.isEmpty()) {
                instance.chMsg = msg;
                instance.enMsg = msg;
            }
        }
        return instance;
    }

    public static ResponseCode instance(String key) {
        if (key == null) {
            return null;
        }
        ResponseCode responseCode = MAP.get(key);
        if (responseCode == null) {
            return null;
        }
        return JSONObject.parseObject(JSONObject.toJSONString(responseCode), ResponseCode.class);
    }

    public String getChMsg() {
        return ((field == null) ? "" : field + " : ") + chMsg;
    }

    public String getEnMsg() {
        return ((field == null) ? "" : field + " : ") + enMsg;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

故事的小黄花-

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

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

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

打赏作者

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

抵扣说明:

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

余额充值