全局异常捕获

我不想学编丿程 / 2024-08-29 / 原文

全局异常处理

@RestControllerAdvice

@RestControllerAdvice 是 Spring Framework 4.0 引入的一个注解,它用于定义一个类,该类可以处理多个类型的控制器的异常和横切关注点(cross-cutting concerns),比如日志记录、安全、数据转换等。这个注解是 @Component 的特化,意味着使用 @RestControllerAdvice 注解的类会自动被 Spring 容器管理,并作为候选 Bean 进行依赖注入。

package com.ruoyi.framework.web.exception;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.ResultData;
import com.ruoyi.common.exception.CustomNormalException;
import com.ruoyi.common.exception.DemoModeException;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理器
 * 
 * @author ruoyi
 */
@RestControllerAdvice
public class GlobalExceptionHandler
{
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);



    /**
     * 业务异常
     */
    @ExceptionHandler(ServiceException.class)
    public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request)
    {
        log.error(e.getMessage(), e);
        Integer code = e.getCode();
        return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage());
    }


    @ExceptionHandler(Exception.class)
    public ResultData<String > handleException(Exception e, HttpServletRequest request)
    {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生系统异常.", requestURI, e);
        return ResultData.fail(e.getMessage());
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public AjaxResult handleBindException(BindException e)
    {
        log.error(e.getMessage(), e);
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return AjaxResult.error(message);
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e)
    {
        log.error(e.getMessage(), e);
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        return AjaxResult.error(message);
    }

    /**
     * 演示模式异常
     */
    @ExceptionHandler(DemoModeException.class)
    public AjaxResult handleDemoModeException(DemoModeException e)
    {
        return AjaxResult.error("演示模式,不允许操作");
    }
}

@ExceptionHandler

在 Spring MVC 中,使用 @ExceptionHandler 注解的方法可以获取多种类型的参数,这些参数提供了关于异常和请求的上下文信息。以下是一些常用的参数:

  1. 异常参数

    • 直接将捕获的异常类型作为方法参数,例如 ServiceException ex
  2. WebRequest 或 HttpServletRequest

    • WebRequest request:提供了关于当前 HTTP 请求的信息。
    • HttpServletRequest request:提供了标准的 Servlet API 请求信息。
  3. HttpServletResponse

    • HttpServletResponse response:允许你自定义响应头或状态码。
  4. Model

    • Model model:可以向模型中添加属性,这些属性将在视图中被渲染。
  5. HttpServletResponse 或 ResponseEntity

    • HttpServletResponse response:用于自定义响应。
    • ResponseEntity<Object> response:用于构建一个包含状态码和响应体的 HTTP 响应。

  6. BindingResult

    • BindingResult result:如果异常是由数据绑定错误引起的,这个参数将包含绑定的结果。
  7. ValidationUtils

    • @Valid YourObject yourObject@Valid YourObject yourObject, BindingResult result:用于验证表单提交的数据。
  8. LocaleResolver

    • LocaleResolver localeResolver:用于获取当前请求的区域设置。
  9. ThemeResolver

    • ThemeResolver themeResolver:用于处理主题相关的逻辑。
  10. PrincipalAuthentication

    • Principal principalAuthentication authentication:用于获取当前用户的认证信息。

这些参数可以单独使用,也可以组合使用,以满足异常处理的需要。以下是一个示例,展示了如何在 @ExceptionHandler 方法中使用一些常见的参数:

@ControllerAdvice
public class CustomExceptionHandler {

    @ExceptionHandler(ServiceException.class)
    public ResponseEntity<Object> handleServiceException(
        ServiceException ex,
        HttpServletRequest request,
        HttpServletResponse response,
        BindingResult result,
        Model model) {

        // 使用参数
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); // 设置状态码
        model.addAttribute("error", ex.getMessage()); // 向模型添加错误信息

        // 构建自定义响应体
        Map<String, Object> responseBody = new HashMap<>();
        responseBody.put("status", "error");
        responseBody.put("message", ex.getMessage());

        return new ResponseEntity<>(responseBody, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

在这个示例中,handleServiceException 方法捕获了 ServiceException 异常,并使用了请求、响应、数据绑定结果和模型参数来构建一个自定义的响应。这允许异常处理方法根据需要获取请求上下文信息,并自定义响应的内容和状态码。

自定义异常

package com.anze.server.utils.error;

public class ServiceException extends  RuntimeException{

    private int code;
    private String message;


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