web 开发后端代码优化

通用返回类

作为所有请求的返回值类型

给要返回的数据补充一些信息,告诉前端这个请求在业务层面上是成功还是失败

{
  "code": 40000,
  "data": null,
  "message": "",
  "description": ""
}
import lombok.Data;
import java.io.Serializable;
/**
 * 通用返回类
 *
 * @param <T>
 * @author longlong
 */
@Data
public class BaseResponse<T> implements Serializable {

    private static final long serialVersionUID = -3546852185606973846L;

    private int code;
    private T data;
    private String message;
    private String description;

    public BaseResponse(int code, T data, String message,String description) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.description = description;
    }
    public BaseResponse(int code, T data, String message){
        this(code,data,message,"");
    }
    public BaseResponse(int code, T data) {
        this(code,data,"","");
    }
    public BaseResponse(ErrorCode errorCode){
        this(errorCode.getCode(),null,errorCode.getMessage(),errorCode.getDescrition());
    }
}

返回工具类

对要返回的数据进行处理

/**
 * 返回工具类
 *
 * @author longlong
 */
public class ResultUtils {

    /**
     * 成功
     * @param data
     * @return
     * @param <T>
     */
    public static<T> BaseResponse<T> success(T data){
        return new BaseResponse<T>(0,data,"ok");
    }

    /**
     * 失败
     * @param errorCode
     * @return
     */
    public static BaseResponse error(ErrorCode errorCode){
        return new BaseResponse(errorCode);
    }

    /**
     * 失败 自定义description
     * @param errorCode
     * @return
     */
    public static BaseResponse error(ErrorCode errorCode,String description){
        return new BaseResponse(errorCode.getCode(),null, errorCode.getMessage(),description);
    }

    public static BaseResponse error(int code,String message,String description){
        return new BaseResponse(code,null, message,description);
    }

    /**
     * 失败 自定义message 和 description
     * @param errorCode
     * @return
     */
    public static BaseResponse error(ErrorCode errorCode,String message, String description){
        return new BaseResponse(errorCode.getCode(),null, message,description);
    }
}

自定义错误码

/**
 * 错误码
 *
 * @author longlong
 */
public enum ErrorCode {

    PARAMS_ERROR(40000,"请求参数错误",""),
    NULL_ERROR(40001,"请求数据为空",""),
    NOT_LOGIN(40100,"未登录",""),
    NO_AUTH(40101,"无权限",""),
    SYSTEM_ERROR(50000,"系统内部异常","")
    ;

    private final int code;
    /**
     * 状态码信息
     */
    private final String message;

    /**
     * 状态码描述 详情
     */
    private final String descrition;

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

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public String getDescrition() {
        return descrition;
    }

自定义异常

/**
 * 自定义异常类
 *
 * @author longlong
 */
public class BusinessException extends RuntimeException{

    private final int code;
    private final String description;

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

    public BusinessException(ErrorCode errorCode){
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
        this.description = errorCode.getDescrition();
    }

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

    public int getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }
}

全局异常处理

作用:

  • 捕获代码中所有的异常,内部消化,让前端得到更详细的业务报错 / 信息
  • 同时屏蔽掉项目框架本身的异常(不暴露服务器内部状态)
  • 集中处理,比如记录日志

实现:

  • Spring AOP:在调用方法前后进行额外的处理
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e){
        log.error("businessException: "+e.getMessage()+"-"+e.getDescription(),e);
        return ResultUtils.error(e.getCode(),e.getMessage(),e.getDescription());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse runtimeExceptionHandler(RuntimeException e){
        log.error("runtimeException: ",e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR);
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值