1. 为什么需要使用全局捕获异常?
请看一下案列,这个是Controller层,
@RestController
public class ErrorController {
// private static final Logger logger =
// LoggerFactory.getLogger(JspController.class);
// 分布式日志收集系统##
// 全局捕获异常 使用AOP技术,采用异常通知
// 如果每个方法都可能会发生异常,每个方法上都加上try。
@RequestMapping("/getUser")
@ResponseBody
public String getser(int i) {
int j = 1 / i;
return j + " fvsdbf ";
}
@RequestMapping("/getMember")
public String getMember(String name, Integer age) {
// logger.info("name:" + name + ",age:" + age);
return "success";
}
}
当前端请求的参数i等于0的时候,这个时候就会出现异常,如下图所示:
这种异常的报告方式显然很不容易被用户接受,也显得很不专业,因此需要使用更加专业的异常报告方式。很多人就会说使用try····catch··进行异常的捕获,如下面的代码所示:
解决效果如下图所示:
但是这种解决方式不是很好。
因此我们就可以采用全局捕获异常的这个技术,整个项目请求全局捕获异常。
全局捕获异常的思路是什么?
使用AOP技术,异常通知的方式进行,通过新建一个类进行异常的捕获
第一种:捕获返回json格式的
第二种:捕获返回页面(比如:html,jsp等页面的异常)
具体代码如下:
/*[email protected](basePackages = "com.itmayiedu.controller") -----这个代表springboot中的异常切入点,后面会跟扫包范围,最好是在controller的包中进行监控
// @ResponseBody 返回json格式
// modeAndView 返回页面
---------------*/
@ControllerAdvice(basePackages = "com.itmayiedu.controller")
public class GlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public Map<String, Object> errorResult() {
// 实际开发中,怎么 将错误记录在日志中。
Map<String, Object> errorResultMap = new HashMap<String, Object>();
errorResultMap.put("errorCode", "500");
errorResultMap.put("errorMsg", "全局捕获异常系统错误!");
return errorResultMap;
}
}
但是,我们不能再使用try-----catch,必须使用原生的控制层代码。
@RestController
public class ErrorController {
// private static final Logger logger =
// LoggerFactory.getLogger(JspController.class);
// 分布式日志收集系统##
// 全局捕获异常 使用AOP技术,采用异常通知
// 如果每个方法都可能会发生异常,每个方法上都加上try。
@RequestMapping("/getUser")
@ResponseBody
public String getser(int i) {
int j = 1 / i;
return j + " fvsdbf ";
}
@RequestMapping("/getMember")
public String getMember(String name, Integer age) {
// logger.info("name:" + name + ",age:" + age);
return "success";
}
}
二、更详细的处理方式介绍(该处为借鉴别人)
Spring 统一异常处理有 3 种方式,分别为:
1.使用 @ ExceptionHandler 注解
2.实现 HandlerExceptionResolver 接口
3.使用 @controlleradvice 注解
我们知道,系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。在开发中,不管是dao层、service层还是controller层,都有可能抛出异常,在springmvc中,能将所有类型的异常处理从各处理过程解耦出来,既保证了相关处理过程的功能较单一,也实现了异常信息的统一处理和维护。
(一) SimpleMappingExceptionResolver
使用这种方式具有集成简单、有良好的扩展性、对已有代码没有入侵性等优点,但该方法仅能获取到异常信息,若在出现异常时,对需要获取除异常以外的数据的情况不适用。
1 @Configuration
2 @EnableWebMvc
3 @ComponentScan(basePackages = {"com.balbala.mvc.web"})
4 public class WebMVCConfig extends WebMvcConfigurerAdapter{
5 @Bean
6 public SimpleMappingExceptionResolver simpleMappingExceptionResolver()
7 {
8 SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver();
9 Properties mappings = new Properties();
10 mappings.put("org.springframework.web.servlet.PageNotFound", "page-404");
11 mappings.put("org.springframework.dao.DataAccessException", "data-access");
12 mappings.put("org.springframework.transaction.TransactionException", "transaction-Failure");
13 b.setExceptionMappings(mappings);
14 return b;
15 }
16 }
(二) HandlerExceptionResolver
相比第一种来说,HandlerExceptionResolver能准确显示定义的异常处理页面,达到了统一异常处理的目标
1.定义一个类实现HandlerExceptionResolver接口
1 public class GlobalHandlerExceptionResolver implements HandlerExceptionResolver {
2 private static final Logger LOG = LoggerFactory.getLogger(GlobalHandlerExceptionResolver.class);
3 /**
4 * 在这里处理所有得异常信息
5 */
6 @Override
7 public ModelAndView resolveException(HttpServletRequest req, HttpServletResponse resp, Object o, Exception ex) {
8 ex.printStackTrace();
9 if (ex instanceof AthenaException) {
10 //AthenaException为一个自定义异常
11 ex.printStackTrace();
12 printWrite(ex.toString(), resp);
13 return new ModelAndView();
14 }
15 //RspMsg为一个自定义处理异常信息的类
16 //ResponseCode为一个自定义错误码的接口
17 RspMsg unknownException = null;
18 if (ex instanceof NullPointerException) {
19 unknownException = new RspMsg(ResponseCode.CODE_UNKNOWN, "业务判空异常", null);
20 } else {
21 unknownException = new RspMsg(ResponseCode.CODE_UNKNOWN, ex.getMessage(), null); }
22 printWrite(unknownException.toString(), resp);
23 return new ModelAndView();
24 }
25
26 /**
27 * 将错误信息添加到response中
28 *
29 * @param msg
30 * @param response
31 * @throws IOException
32 */
33 public static void printWrite(String msg, HttpServletResponse response) {
34 try {
35 PrintWriter pw = response.getWriter();
36 pw.write(msg);
37 pw.flush();
38 pw.close();
39 } catch (Exception e) {
40 e.printStackTrace();
41 }
42 }
43 }
这种方式实现的异常处理,可以针对不同的异常和自己定义的异常码进行翻译,然后输出到response中,在前端展示。
(三)@ExceptionHandler
1.首先定义一个父类,实现一些基础的方法
1 public class BaseGlobalExceptionHandler {
2 protected static final Logger logger = null;
3 protected static final String DEFAULT_ERROR_MESSAGE = "系统忙,请稍后再试";
4
5 protected ModelAndView handleError(HttpServletRequest req, HttpServletResponse rsp, Exception e, String viewName, HttpStatus status) throws Exception {
6 if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null)
7 throw e;
8 String errorMsg = e instanceof MessageException ? e.getMessage() : DEFAULT_ERROR_MESSAGE;
9 String errorStack = Throwables.getStackTraceAsString(e);
10
11 getLogger().error("Request: {} raised {}", req.getRequestURI(), errorStack);
12 if (Ajax.isAjax(req)) {
13 return handleAjaxError(rsp, errorMsg, status);
14 }
15 return handleViewError(req.getRequestURL().toString(), errorStack, errorMsg, viewName);
16 }
17
18 protected ModelAndView handleViewError(String url, String errorStack, String errorMessage, String viewName) {
19 ModelAndView mav = new ModelAndView();
20 mav.addObject("exception", errorStack);
21 mav.addObject("url", url);
22 mav.addObject("message", errorMessage);
23 mav.addObject("timestamp", new Date());
24 mav.setViewName(viewName);
25 return mav;
26 }
27
28 protected ModelAndView handleAjaxError(HttpServletResponse rsp, String errorMessage, HttpStatus status) throws IOException {
29 rsp.setCharacterEncoding("UTF-8");
30 rsp.setStatus(status.value());
31 PrintWriter writer = rsp.getWriter();
32 writer.write(errorMessage);
33 writer.flush();
34 return null;
35 }
36
37 public Logger getLogger() {
38 return LoggerFactory.getLogger(BaseGlobalExceptionHandler.class);
39 }
40 }
2.针对你需要捕捉的异常实现相对应的处理方式
1 @ControllerAdvice
2 public class GlobalExceptionHandler extends BaseGlobalExceptionHandler {
3
4 //比如404的异常就会被这个方法捕获
5 @ExceptionHandler(NoHandlerFoundException.class)
6 @ResponseStatus(HttpStatus.NOT_FOUND)
7 public ModelAndView handle404Error(HttpServletRequest req, HttpServletResponse rsp, Exception e) throws Exception {
8 return handleError(req, rsp, e, "error-front", HttpStatus.NOT_FOUND);
9 }
10
11 //500的异常会被这个方法捕获
12 @ExceptionHandler(Exception.class)
13 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
14 public ModelAndView handleError(HttpServletRequest req, HttpServletResponse rsp, Exception e) throws Exception {
15 return handleError(req, rsp, e, "error-front", HttpStatus.INTERNAL_SERVER_ERROR);
16 }
17
18 //TODO 你也可以再写一个方法来捕获你的自定义异常
19 //TRY NOW!!!
20
21 @Override
22 public Logger getLogger() {
23 return LoggerFactory.getLogger(GlobalExceptionHandler.class);
24 }
25
26 }