java-project 公共返回 ENMU & BusinessCode

本文介绍了Java中用于响应式框架的RetVO类,它包含状态码、消息和数据字段,用于构建统一的返回结果。同时,还定义了BusinessException,用于处理业务异常,包含了自定义的错误码和错误信息。这些组件在处理前后端交互时提供了标准化的错误处理机制。

code enum:


import java.io.Serializable;

/**
 * @author tong.luo
 * @description RetVO
 * @date 2021/3/8 17:07
 */
public class RetVO<T> implements Serializable {
    private static final long serialVersionUID = 4087908700685218023L;

    private String code;
    private String msg;
    private T data;

    public RetVO() {
    }

    private RetVO(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private RetVO(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private RetVO(T data) {
        this.code = SysEnum.SUCCESS.getCode();
        this.msg = SysEnum.SUCCESS.getMsg();
        this.data = data;
    }

    public static <T> RetVO<T> success(T data) {
        return new RetVO<>(data);
    }

    public static <T> RetVO<T> error(String msg, String code) {
        return new RetVO<>(SysEnum.SUCCESS.getCode(), msg);
    }

    public static <T> RetVO<T> error(SysEnum sysEnum) {
        return new RetVO<>(sysEnum.getCode(), sysEnum.getMsg());
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

public enum SysEnum {
    SUCCESS("200","success"),
    error("500","server error"),
    ;
    private String code;
    private String msg;

    SysEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

/**
 * @author tong.luo
 * @description BusinessException
 * @date 2021/3/8 17:14
 */
public class BusinessException extends RuntimeException {
    private static final long serialVersionUID = -6733364661567308818L;
    private Integer code;
    private String message;

    public BusinessException(String message) {
        super(message);
        this.message = message;
    }

    public BusinessException(Integer code, String msg) {
        super(msg);
        this.code = code;
        this.message = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Diligently_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值