通用返回类

通用返回类

Result.java

package com.ghr.common;

import lombok.Data;

import java.io.Serializable;

/**
 * @author Gghr
 * @version 1.0
 * @date 2022/12/18 0:39
 */

/**
 * 通用返回类
 * @param <T>
 */
@Data
public class Result<T> implements Serializable {

     private int code;

     private T data;

     private String message;

     private String description;

    public Result(int code, T data, String message, String description) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.description = description;
    }

    public Result(ErrorCode errorCode, ErrorDesc errorDesc) {
        this(errorCode.getCode(), null,errorCode.getMessage(), errorDesc.getDescription());
    }

    public Result(int code, T data, String message) {
        this(code, data ,message, "");
    }

    public Result(int code, T data) {
        this(code, data ,"", "");
    }

}

ErrorCode.java

package com.ghr.common;

/**
 * @author Gghr
 * @version 1.0
 * @date 2023/7/18 21:29
 */
public enum ErrorCode {
    SUCCESS(0, "ok"),
    PARAMS_ERROR(40000, "请求参数错误"),
    NULL_ERROR(40001, "请求参数为空"),
    REQ_ERROR(40001, "请求数据错误"),
    RESP_ERROR(40002,"响应数据错误"),
    RESP_NULL_ERROR(40002,"响应数据为空"),
    NOT_LOGIN(40100, "未登录"),
    NO_AUTH(40100, "无权限"),
    FORBIDDEN(40300, "无权限"),
    SYSTEM_ERROR(50000, "系统内部异常"),


;
    private final int code;
    private final String message;

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    ErrorCode(int code, String message){
        this.code = code;
        this.message = message;
    }
}

ErrorDesc.java

package com.ghr.common;

/**
 * @author Gghr
 * @version 1.0
 * @date 2023/7/18 21:31
 */
public enum ErrorDesc {

    DEFAULT_ERROR("失败"),

    SAVE_ERROR("插入数据失败!"),
    DELETE_ERROR("删除数据失败!"),
    UPDATE_ERROR("更新数据失败"),
    GET_ERROR("查询数据失败"),
    ;


    private final String description;

    public String getDescription() {
        return description;
    }

    ErrorDesc(String description) {
        this.description = description;
    }
}

ResultUtils.java

package com.ghr.common;

/**
 * @author Gghr
 * @version 1.0
 * @date 2022/12/18 11:32
 */
@SuppressWarnings(value = "all")
public class ResultUtils {

    public static <T> Result<T> success(T data){
        return new Result<>(200, data,"success");
    }

    public static <T> Result<T> error(int code, String message){
        return new Result(code, null, message);
    }

    public static <T> Result<T> error(int code, String message, String description){
        return new Result(code, null, message, description);
    }

    public static <T> Result<T> error(ErrorCode errorCode, String message){
        return new Result(errorCode.getCode(), null, message);
    }

    public static <T> Result<T> error(ErrorCode errorCode, ErrorDesc errorDesc){
        return new Result<>(errorCode.getCode(),null, errorCode.getMessage(), errorDesc.getDescription());
    }

}

全局异常处理

BusinessException.java

package com.ghr.exception;

import com.ghr.common.ErrorCode;
import com.ghr.common.ErrorDesc;

/**
 * @author Gghr
 * @version 1.0
 * @date 2023/7/19 21:28
 */
public class BusinessException extends RuntimeException {
    private final int code;
    private final String description;

    public int getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    public BusinessException(int code, String message, String description) {
        super(message);
        this.code = code;
        this.description = description;
    }

    public BusinessException(ErrorCode errorCode, ErrorDesc errorDesc){
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
        this.description = errorDesc.getDescription();
    }

}

GlobalExceptionHandler.java

package com.ghr.exception;

import com.ghr.common.ErrorCode;
import com.ghr.common.Result;
import com.ghr.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Gghr
 * @version 1.0
 * @date 2022/12/18 15:00
 */

/**
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public Result businessExceptionHandle(BusinessException e){
        log.error("BusinessException: " + e.getMessage(), e);
        return ResultUtils.error(e.getCode(), e.getMessage(), e.getDescription());

    }

    @ExceptionHandler(RuntimeException.class)
    public Result runtimeExceptionHandle(RuntimeException e){
        log.error("RuntimeException: ", e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR, e.getMessage());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值