Spring Cloud Spring Boot mybatis分布式微服务云架构(十一)Web应用的统一异常处理
2018-03-01 00:00
1191 查看
我们在做Web应用的时候,请求处理过程中发生错误是非常常见的情况。Spring Boot提供了一个默认的映射:
选择一个之前实现过的Web应用(Chapter3-1-2)为基础,启动该应用,访问一个不存在的URL,或是修改处理内容,直接抛出异常,如:
此时,可以看到类似下面的报错页面,该页面就是Spring Boot提供的默认error映射页面。
alt=默认的错误页面
下面我们以之前的Web应用例子为基础(Chapter3-1-2),进行统一异常处理的改造。
创建全局异常处理类:通过使用
实现
alt=自定义的错误页面
通过实现上述内容之后,我们只需要在
源码来源
/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来展示异常内容。
选择一个之前实现过的Web应用(Chapter3-1-2)为基础,启动该应用,访问一个不存在的URL,或是修改处理内容,直接抛出异常,如:
@RequestMapping("/hello") public String hello() throws Exception { throw new Exception("发生错误"); }
此时,可以看到类似下面的报错页面,该页面就是Spring Boot提供的默认error映射页面。
alt=默认的错误页面
统一异常处理
虽然,Spring Boot中实现了默认的error映射,但是在实际应用中,上面你的错误页面对用户来说并不够友好,我们通常需要去实现我们自己的异常提示。下面我们以之前的Web应用例子为基础(Chapter3-1-2),进行统一异常处理的改造。
创建全局异常处理类:通过使用
@ControllerAdvice定义统一的异常处理类,而不是在每个Controller中逐个定义。
@ExceptionHandler用来定义函数针对的异常类型,最后将Exception对象和请求URL映射到
error.html中
@ControllerAdvice class GlobalExceptionHandler { public static final String DEFAULT_ERROR_VIEW = "error"; @ExceptionHandler(value = Exception.class) public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { ModelAndView mav = new ModelAndView(); mav.addObject("exception", e); mav.addObject("url", req.getRequestURL()); mav.setViewName(DEFAULT_ERROR_VIEW); return mav; } }
实现
error.html页面展示:在
templates目录下创建
error.html,将请求的URL和Exception对象的message输出。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title>统一异常处理</title> </head> <body> <h1>Error Handler</h1> <div th:text="${url}"></div> <div th:text="${exception.message}"></div> </body> </html>启动该应用,访问:
http://localhost:8080/hello,可以看到如下错误提示页面。
alt=自定义的错误页面
通过实现上述内容之后,我们只需要在
Controller中抛出
Exception,当然我们可能会有多种不同的
Exception。然后在
@ControllerAdvice类中,根据抛出的具体
Exception类型匹配
@ExceptionHandler中配置的异常类型来匹配错误映射和处理。
源码来源
相关文章推荐
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十一)Web应用的统一异常处理
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十一)Web应用的统一异常处理
- SpringCloud SpringBoot mybatis 分布式微服务(六)Spring Boot中Web应用的统一异常处理
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四十)使用AOP统一处理Web请求日志(1)
- 企业分布式微服务云SpringCloud SpringBoot mybatis (五)Spring Boot中Web应用的统一异常处理
- 企业分布式微服务云SpringCloud SpringBoot mybatis (五)Spring Boot中Web应用的统一异常处理
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四十一)实现Web层的日志切面
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十三)使用MongoDB数据库(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三)属性配置文件详解(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二)使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程
- Spring Cloud Spring Boot mybatis分布式微服务云架构(九)使用Swagger2构建强大的RESTful API文档(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十三)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十六)使用JdbcTemplate操作数据库
- Spring Cloud Spring Boot mybatis分布式微服务云架构(九)使用Swagger2构建强大的RESTful API文档(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二十六)使用MyBatis注解配置详解(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三)属性配置文件详解(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四)属性配置文件详解(2)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三十四)注解配置与EhCache使用(2)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四)属性配置文件详解(2)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三十一)事务管理(1)