全局异常捕获
全局异常处理
@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 注解的方法可以获取多种类型的参数,这些参数提供了关于异常和请求的上下文信息。以下是一些常用的参数:
-
异常参数:
- 直接将捕获的异常类型作为方法参数,例如
ServiceException ex。
- 直接将捕获的异常类型作为方法参数,例如
-
WebRequest 或 HttpServletRequest:
WebRequest request:提供了关于当前 HTTP 请求的信息。HttpServletRequest request:提供了标准的 Servlet API 请求信息。
-
HttpServletResponse:
HttpServletResponse response:允许你自定义响应头或状态码。
-
Model:
Model model:可以向模型中添加属性,这些属性将在视图中被渲染。
-
HttpServletResponse 或 ResponseEntity

浙公网安备 33010602011771号