Springboot项目全局异常统一处理案例代码
作者:hao_kkkkk 时间:2021-08-26 10:51:19
最近在做项目时需要对异常进行全局统一处理,主要是一些分类入库以及记录日志等,因为项目是基于Springboot的,所以去网络上找了一些博客文档,然后再结合项目本身的一些特殊需求做了些许改造,现在记录下来便于以后查看。
在网络上找到关于Springboot全局异常统一处理的文档博客主要是两种方案:
1、基于@ControllerAdvice注解的Controller层的全局异常统一处理
以下是网上一位博主给出的代码示例,该博客地址为:https://www.jb51.net/article/195669.htm
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
* controller 增强器
*
* @author sam
* @since 2017/7/17
*/
@ControllerAdvice
public class MyControllerAdvice {
/**
* 全局异常捕捉处理
* @param ex
* @return
*/
@ResponseBody
@ExceptionHandler(value = Exception.class)
public Map errorHandler(Exception ex) {
Map map = new HashMap();
map.put("code", 100);
map.put("msg", ex.getMessage());
return map;
}
/**
* 拦截捕捉自定义异常 MyException.class
* @param ex
* @return
*/
@ResponseBody
@ExceptionHandler(value = MyException.class)
public Map myErrorHandler(MyException ex) {
Map map = new HashMap();
map.put("code", ex.getCode());
map.put("msg", ex.getMsg());
return map;
}
}
这个代码示例写的非常浅显易懂,但是需要注意的是:基于@ControllerAdvice注解的全局异常统一处理只能针对于Controller层的异常,意思是只能捕获到Controller层的异常,在service层或者其他层面的异常都不能捕获。
根据这段示例代码以及结合项目本身的实际需求,对该实例代码做了稍微改造(其实几乎没做改造,只是业务处理不一样而已):
@ControllerAdvice
public class AdminExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(AdminExceptionHandler.class);
/**
* @Author: gmy
* @Description: 系统异常捕获处理
* @Date: 16:07 2018/5/30
*/
@ResponseBody
@ExceptionHandler(value = Exception.class)
public APIResponse javaExceptionHandler(Exception ex) {//APIResponse是项目中对外统一的出口封装,可以根据自身项目的需求做相应更改
logger.error("捕获到Exception异常",ex);
//异常日志入库
return new APIResponse(APIResponse.FAIL,null,ex.getMessage());
}
/**
* @Author: gmy
* @Description: 自定义异常捕获处理
* @Date: 16:08 2018/5/30
*/
@ResponseBody
@ExceptionHandler(value = MessageCenterException.class)//MessageCenterException是自定义的一个异常
public APIResponse messageCenterExceptionHandler(MessageCenterException ex) {
logger.error("捕获到MessageCenterException异常",ex.getException());
//异常日志入库
return ex.getApiResponse();
}
}
public class MessageCenterException extends RuntimeException {
public MessageCenterException(APIResponse apiResponse, Exception exception){
this.apiResponse = apiResponse;
this.exception = exception;
}
private Exception exception;
private APIResponse apiResponse;
public Exception getException() {
return exception;
}
public void setException(Exception exception) {
this.exception = exception;
}
public APIResponse getApiResponse() {
return apiResponse;
}
public void setApiResponse(APIResponse apiResponse) {
this.apiResponse = apiResponse;
}
}
经过测试发现可以捕获到Controller层的异常,当前前提是Controller层没有对异常进行catch处理,如果Controller层对异常进行了catch处理,那么在这里就不会捕获到Controller层的异常了,所以这一点要特别注意。
在实际测试中还发现,如果在Controller中不做异常catch处理,在service中抛出异常(service中也不错异常catch处理),那么也是可以在这里捕获到异常的。
2、基于Springboot自身的全局异常统一处理,主要是实现ErrorController接口或者继承AbstractErrorController抽象类或者继承BasicErrorController类
以下是网上一位博主给出的示例代码,博客地址为:https://www.jb51.net/article/110536.htm
@Controller
@RequestMapping(value = "error")
@EnableConfigurationProperties({ServerProperties.class})
public class ExceptionController implements ErrorController {
private ErrorAttributes errorAttributes;
@Autowired
private ServerProperties serverProperties;
/**
* 初始化ExceptionController
* @param errorAttributes
*/
@Autowired
public ExceptionController(ErrorAttributes errorAttributes) {
Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
this.errorAttributes = errorAttributes;
}
/**
* 定义404的ModelAndView
* @param request
* @param response
* @return
*/
@RequestMapping(produces = "text/html",value = "404")
public ModelAndView errorHtml404(HttpServletRequest request,
HttpServletResponse response) {
response.setStatus(getStatus(request).value());
Map<String, Object> model = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.TEXT_HTML));
return new ModelAndView("error/404", model);
}
/**
* 定义404的JSON数据
* @param request
* @return
*/
@RequestMapping(value = "404")
@ResponseBody
public ResponseEntity<Map<String, Object>> error404(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.TEXT_HTML));
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}
/**
* 定义500的ModelAndView
* @param request
* @param response
* @return
*/
@RequestMapping(produces = "text/html",value = "500")
public ModelAndView errorHtml500(HttpServletRequest request,
HttpServletResponse response) {
response.setStatus(getStatus(request).value());
Map<String, Object> model = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.TEXT_HTML));
return new ModelAndView("error/500", model);
}
/**
* 定义500的错误JSON信息
* @param request
* @return
*/
@RequestMapping(value = "500")
@ResponseBody
public ResponseEntity<Map<String, Object>> error500(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.TEXT_HTML));
HttpStatus status = getStatus(request);
return new ResponseEntity<Map<String, Object>>(body, status);
}
/**
* Determine if the stacktrace attribute should be included.
* @param request the source request
* @param produces the media type produced (or {@code MediaType.ALL})
* @return if the stacktrace attribute should be included
*/
protected boolean isIncludeStackTrace(HttpServletRequest request,
MediaType produces) {
ErrorProperties.IncludeStacktrace include = this.serverProperties.getError().getIncludeStacktrace();
if (include == ErrorProperties.IncludeStacktrace.ALWAYS) {
return true;
}
if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM) {
return getTraceParameter(request);
}
return false;
}
/**
* 获取错误的信息
* @param request
* @param includeStackTrace
* @return
*/
private Map<String, Object> getErrorAttributes(HttpServletRequest request,
boolean includeStackTrace) {
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
return this.errorAttributes.getErrorAttributes(requestAttributes,
includeStackTrace);
}
/**
* 是否包含trace
* @param request
* @return
*/
private boolean getTraceParameter(HttpServletRequest request) {
String parameter = request.getParameter("trace");
if (parameter == null) {
return false;
}
return !"false".equals(parameter.toLowerCase());
}
/**
* 获取错误编码
* @param request
* @return
*/
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request
.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
try {
return HttpStatus.valueOf(statusCode);
}
catch (Exception ex) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
/**
* 实现错误路径,暂时无用
* @see ExceptionMvcAutoConfiguration#containerCustomizer()
* @return
*/
@Override
public String getErrorPath() {
return "";
}
}
该示例写的也是非常简单明了的,但是结合本身项目的实际需求,也是不能直接拿来用的,需要做相应的改造,改造主要有以下方面:
1、因为项目是前后端分离的,所以Controller层不会有ModelAndView返回类型,需要返回自身的APIResponse返回类型
2、项目需要统计全部的异常,而不只是404或者500的异常
3、捕获到异常之后需要做特殊化的业务处理
所以基于以上几方面对示例代码做了改造,具体改造代码如下:
/**
* @Author: gmy
* @Description: Springboot全局异常统一处理
* @Date: 2018/5/30
* @Time: 16:41
*/
@RestController
@EnableConfigurationProperties({ServerProperties.class})
public class ExceptionController implements ErrorController {
private ErrorAttributes errorAttributes;
@Autowired
private ServerProperties serverProperties;
/**
* 初始化ExceptionController
* @param errorAttributes
*/
@Autowired
public ExceptionController(ErrorAttributes errorAttributes) {
Assert.notNull(errorAttributes, "ErrorAttributes must not be null");
this.errorAttributes = errorAttributes;
}
@RequestMapping(value = "/error")
@ResponseBody
public APIResponse error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.ALL));
HttpStatus status = getStatus(request);
return new APIResponse(APIResponse.FAIL,null,body.get("message").toString());
}
/**
* Determine if the stacktrace attribute should be included.
* @param request the source request
* @param produces the media type produced (or {@code MediaType.ALL})
* @return if the stacktrace attribute should be included
*/
protected boolean isIncludeStackTrace(HttpServletRequest request,
MediaType produces) {
ErrorProperties.IncludeStacktrace include = this.serverProperties.getError().getIncludeStacktrace();
if (include == ErrorProperties.IncludeStacktrace.ALWAYS) {
return true;
}
if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM) {
return getTraceParameter(request);
}
return false;
}
/**
* 获取错误的信息
* @param request
* @param includeStackTrace
* @return
*/
private Map<String, Object> getErrorAttributes(HttpServletRequest request,
boolean includeStackTrace) {
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
return this.errorAttributes.getErrorAttributes(requestAttributes,
includeStackTrace);
}
/**
* 是否包含trace
* @param request
* @return
*/
private boolean getTraceParameter(HttpServletRequest request) {
String parameter = request.getParameter("trace");
if (parameter == null) {
return false;
}
return !"false".equals(parameter.toLowerCase());
}
/**
* 获取错误编码
* @param request
* @return
*/
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request
.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
try {
return HttpStatus.valueOf(statusCode);
}
catch (Exception ex) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
}
/**
* 实现错误路径,暂时无用
* @return
*/
@Override
public String getErrorPath() {
return "";
}
}
经过测试,可以捕获到所有层面上的异常,当前前提仍然是没有对异常进行catch处理,否则这里也是捕获不到
来源:https://blog.csdn.net/hao_kkkkk/article/details/80538955
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java SpringBoot启动指定profile的8种方式详解
![](https://img.aspxhome.com/file/2023/1/57831_0s.jpg)
Android 文件数据存储实例详解
Java中比较运算符compareTo()、equals()与==的区别及应用总结
![](https://img.aspxhome.com/file/2023/1/60801_0s.jpg)
SpringBoot日志框架之Log4j2快速入门与参数详解
![](https://img.aspxhome.com/file/2023/3/61653_0s.webp)
高斯混合模型与EM算法图文详解
![](https://img.aspxhome.com/file/2023/5/61855_0s.jpg)
C语言实现两个矩阵相乘
![](https://img.aspxhome.com/file/2023/6/103076_0s.png)
java数据库唯一id生成工具类
java异步上传图片示例
Java实现添加,读取和删除Excel图片的方法详解
![](https://img.aspxhome.com/file/2023/8/60328_0s.png)
简单解析java方法在调用在内存中的执行过程
![](https://img.aspxhome.com/file/2023/6/59756_0s.png)
Java实现简单树结构
使用栈的迷宫算法java版代码
logback输出日志屏蔽quartz的debug等级日志方式
深入理解ThreadLocal工作原理及使用示例
![](https://img.aspxhome.com/file/2023/6/62066_0s.png)
教你怎么用Java数组和链表实现栈
![](https://img.aspxhome.com/file/2023/0/58720_0s.png)
flutter窗口初始和绘制流程详析
java控制台输出图书馆管理系统
![](https://img.aspxhome.com/file/2023/4/61284_0s.jpg)