Spring全局统一异常处理


前言

我们开发项目的时候,数据在请求过程中发生错误是非常常见的事情。
如:权限不足、数据唯一异常、数据不能为空异常、业务异常等。 这些异常如果不经过处理会对前端开发人员和使用者造成不便,因此我们就需要统一处理他们。

一、异常实体

package com.example.demo.exception;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * @author Clang
 * @version 1.0
 * @date 2022/6/6 15:05
 */
@Data
public class ApiError {
    
    
    private Integer status;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime timestamp;
    private String message;

    private ApiError() {
    
    
        timestamp = LocalDateTime.now();
    }

    public ApiError(Integer status, String message) {
    
    
        this();
        this.status = status;
        this.message = message;
    }
}

二、自定义异常类

封装了 BadRequestException,用于处理通用的异常

package com.example.demo.exception;

import lombok.Getter;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.BAD_REQUEST;

/**
 * @author Clang
 * @version 1.0
 * @date 2022/6/6 15:00
 */
@Getter
public class BadRequestException extends RuntimeException {
    
    
    private Integer status = BAD_REQUEST.value();

    public BadRequestException(String msg) {
    
    
        super(msg);
    }

    public BadRequestException(HttpStatus status, String msg) {
    
    
        super(msg);
        this.status = status.value();
    }
}

三、全局异常拦截

使用全局异常处理器 @RestControllerAdvice 处理请求发送的异常

  • @RestControllerAdvice:默认会扫描指定包中所有@RequestMapping注解
  • @ExceptionHandler:通过@ExceptionHandler的 value 属性可过滤拦截的条件
package com.example.demo.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static org.springframework.http.HttpStatus.BAD_REQUEST;

/**
 * @author Clang
 * @version 1.0
 * @date 2022/6/6 15:04
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    
    
    /**
     * 处理所有不可知的异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Throwable.class)
    public ResponseEntity handleException(Throwable e) {
    
    
        ApiError apiError = new ApiError(BAD_REQUEST.value(), e.getMessage());
        return buildResponseEntity(apiError);
    }

    /**
     * 处理自定义异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = BadRequestException.class)
    public ResponseEntity<ApiError> badRequestException(BadRequestException e) {
    
    
        ApiError apiError = new ApiError(e.getStatus(), e.getMessage());
        return buildResponseEntity(apiError);
    }

    /**
     * 统一返回
     *
     * @param apiError
     * @return
     */
    private ResponseEntity<ApiError> buildResponseEntity(ApiError apiError) {
    
    
        return new ResponseEntity(apiError, HttpStatus.valueOf(apiError.getStatus()));
    }
}

四、具体使用

throw new BadRequestException(HttpStatus.OK, "发送了异常");

提示:更多内容可以访问Clang’s Blog:https://www.clang.asia

猜你喜欢

转载自blog.csdn.net/u012899618/article/details/125147290