SpringMVC设置全局异常处理器的步骤
背景
在项目中我们有需求做一个全局异常处理,来规范所有出去的异常信息。
参考:官方文档
分析
首先 ControllerAdvice(RestControllerAdvice ) ,ControllerAdvice 是无法处理过滤器和拦截器中的异常的。
引用一张图

下面介绍controller层的全局异常设置
全局异常处理也有多种方式
使用@ControllerAdvice(@RestControllerAdvice)+@ExceptionHandler实现全局异常
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
/**
* 处理参数错误的异常
* @param e
* @return
*/
@ResponseBody
@ExceptionHandler(value = IllegalParamsException.class)
public ResultVO<Object> handleIllegalParamsException(IllegalParamsException e) {
ResultVO<Object> resultVo = new ResultVO<>();
resultVo.setStatus(HttpStatus.BAD_REQUEST.value());
resultVo.setErrorCode(e.getErrorInfo().getErrorCode());
resultVo.setErrorMsg(e.getErrorInfo().getErrorDesc());
return resultVo;
}
@ResponseBody
@ExceptionHandler(value = Exception.class)
public ResultVO<Object> handleException(Exception e) {
ResultVO<Object> resultVo = new ResultVO<>();
resultVo.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
resultVo.setErrorMsg(e.getMessage());
return resultVo;
}
}@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultVO<T> {
private Integer status;
private String errorCode;
private String errorMsg;
private T data;
public ResultVO(Integer status, String errorCode, String errorMsg) {
this.status = status;
this.errorCode = errorCode;
this.errorMsg = errorMsg;
}
}public class IllegalParamsException extends RuntimeException {
private static final long serialVersionUID = -6298406656682893468L;
private OperationErrorEnum errorInfo;
public IllegalParamsException(OperationErrorEnum errorInfo) {
this.errorInfo = errorInfo;
}
public IllegalParamsException(String message, OperationErrorEnum errorInfo) {
super(message);
this.errorInfo = errorInfo;
}
public IllegalParamsException(String message, Throwable cause, OperationErrorEnum errorInfo) {
super(message, cause);
this.errorInfo = errorInfo;
}
public IllegalParamsException(Throwable cause, OperationErrorEnum errorInfo) {
super(cause);
this.errorInfo = errorInfo;
}
public IllegalParamsException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, OperationErrorEnum errorInfo) {
super(message, cause, enableSuppression, writableStackTrace);
this.errorInfo = errorInfo;
}
public OperationErrorEnum getErrorInfo() {
return errorInfo;
}
}全局异常处理-多个处理器匹配顺序
参考:参考
多个处理器的两种情况:
存在一个类中
子类异常处理器优先
存在不同的类中
与多个异常处理类放入LinkedHashMap的顺序有关,
可以利用Order指定顺序,如果没有,则默认最小顺序;
那么,如果都没有指定顺序的话,那就是list中的顺序
对于过滤器和拦截器中的异常,有两种思路可以考虑
1、catch后通过转发到异常页面(设置ModelAndView)
参考:参考
2、拦截器中发生异常,拦截器中直接返回错误(通过response.getOutputStream().write() 直接写错误信息)
如:
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
try {
// 业务代码
} catch (Exception e) {
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
ResultVO<Object> resultVo = new ResultVO<>();
resultVo.setStatus(HttpStatus.UNAUTHORIZED.value());
resultVo.setErrorMsg(ACCESS_PARAM_ERROR.getErrorDesc());
response.getOutputStream().write(new String(JSON.toJSONString(resultVo)).getBytes(StandardCharsets.UTF_8));
logger.error("==== WhiteListAndAuthenticationInterceptor拦截器拦截到了方法:{} 解析鉴权参数异常 ====", methodName);
return false;
}
}到此这篇关于SpringMVC设置全局异常处理器的步骤的文章就介绍到这了,更多相关SpringMVC全局异常处理器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Mybatis-plus使用wrapper多表内连接左连接查询方式
文章介绍了使用MyBatis-Plus扩展包进行多表查询的方法,首先,通过DAO层使用注解和自定义SQL段实现关联查询;其次,在Service层通过QueryWrapper添加查询条件;最后在Controller层实现数据控制2025-10-10
解决java-jar报错:xxx.jar 中没有主清单属性的方法
在使用 java -jar xxx.jar 命令运行 Java 应用程序时,遇到了以下错误:xxx.jar 中没有主清单属性,这个错误表示 JAR 文件缺少必要的启动信息,本文将介绍该错误的原因以及如何通过修改 pom.xml 文件来解决,需要的朋友可以参考下2024-11-11
解决spirngboot连接redis报错:READONLY You can‘t write against
docker部署的redis,springboot基本每天来连redis都报错:READONLY You can't write against a read only replica,重启redis后,可以正常连接。但是每天都重启redis,不现实,也很麻烦,今天给大家分享解决方式,感兴趣的朋友一起看看吧2023-06-06


最新评论