java后端常用返回参数,复制粘贴直接用

@Data
public class CommonResult<T> {
    
    /**
     * 结果
     */
    private T data;
 
    /**
     * 状态码
     */
    private Integer code;
 
    /**
     * 状态码描述
     */
    private String message;

    public CommonResult() {}
 
    public CommonResult(Integer code, String message) {
        this.code = code;
        this.message = message;
    }
 



    protected CommonResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
 
    /**
     * 成功返回结果
     *
     */
    public static <T> CommonResult<T> success() {
        return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getMessage());
    }
 
    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> CommonResult<T> success(T data ) {
        return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), ExceptionCode.SUCCESS.getMessage(), data);
    }



    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     * @param  message 提示信息
     */
    public static <T> CommonResult<T> success(T data, String message) {
        return new CommonResult<>(ExceptionCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     * @param errorCode 错误码
     * @param message 错误信息
     */
    public static <T> CommonResult<T> failed(Integer errorCode, String message) {
        return new CommonResult<>(errorCode, message, null);
    }
 
    /**
     * 失败返回结果
     * @param message 提示信息
     */
    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<>(ExceptionCode.FAILED.getCode(), message, null);
    }

    /**
     * 权限过期
     */
    public static <T> CommonResult<T> unauthorized() {
        return new CommonResult<>(ExceptionCode.FAILED.getCode(), "用户登录已过期,请重新登录!", null);
    }

}
public class ExceptionCode {
 
    public static final ExceptionCode SUCCESS = new ExceptionCode(200, "操作成功");
    public static final ExceptionCode FAILED = new ExceptionCode(500, "系统异常");
 
    private int code;
    private String message;
 
    public ExceptionCode(int code, String message) {
        this.code = code;
        this.message= message;
    }
 
    public ExceptionCode(String message) {
        this.message = message;
    }
 
    public Integer getCode() {
        return this.code;
    }
 
    public String getMessage() {
        return this.message;
    }
 
}

需要更多相干的干货欢迎小伙伴留言或我微信15585764735,直接发送给小伙伴,没套路哦,小编看见了就发哦^_^

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

墨笔之风

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

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

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

打赏作者

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

抵扣说明:

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

余额充值