马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
定义错误页面
SpringBoot 默认的处置处罚非常的机制:SpringBoot 默认的已经提供了一套处置处罚非常的机制。一旦步伐中出现了非常 SpringBoot 会像/error 的 url 发送哀求。在 springBoot 中提供了一个叫 BasicExceptionController 来处置处罚/error 哀求,然后跳转到默认显示非常的页面来展示非常信息
如 果 我 们 需 要 将 所 有 的 异 常 同 一 跳 转 到 自 定 义 的 错 误 页 面 , 需 要 在src/main/resources/templates 目录下创建 error.html 页面。注意:名称必须叫 error
@ExceptionHandler 处置处罚
针对特定的非常做出不同的处置处罚,我们可以通过@ExceptionHandle来处置处罚实现,具体如下
- @Controller
- public class UserController {
- /**
- * 模拟 NullPointerException
- * @return
- */
- @RequestMapping("/show1")
- public String showInfo(){
- String str = null;
- str.length();
- return "index";
- }
- /**
- * 模拟 ArithmeticException
- * @return
- */
- @RequestMapping("/show2")
- public String showInfo2(){
- int a = 10/0;
- return "index";
- }
- /**
- * java.lang.ArithmeticException
- * 该方法需要返回一个 ModelAndView:目的是可以让我们封装异常信息以及视图的指定
- * 参数 Exception e:会将产生异常对象注入到方法中
- */
- @ExceptionHandler(value={java.lang.ArithmeticException.class})
- public ModelAndView arithmeticExceptionHandler(Exception e){
- ModelAndView mv = new ModelAndView();
- mv.addObject("error", e.toString());
- mv.setViewName("error1");
- return mv;
- }
- /**
- * java.lang.NullPointerException
- * 该方法需要返回一个 ModelAndView:目的是可以让我们封装异常信息以及视
- 图的指定
- * 参数 Exception e:会将产生异常对象注入到方法中
- */
- @ExceptionHandler(value={java.lang.NullPointerException.class})
- public ModelAndView nullPointerExceptionHandler(Exception e){
- ModelAndView mv = new ModelAndView();
- mv.addObject("error", e.toString());
- mv.setViewName("error2");
- return mv;
- }
- }
复制代码 错误页面
- <!DOCTYPE html>
- <html lang="en" >
- <head>
- <meta charset="UTF-8">
- <title>错误页面</title>
- </head>
- <body>
- 出错了,请与管理员联系。。。错误提示页面-ArithmeticException<br>
- <span th:text="${error}"></span>
- </body>
- </html>
复制代码- <!DOCTYPE html>
- <html lang="en" >
- <head>
- <meta charset="UTF-8">
- <title>错误页面</title>
- </head>
- <body>
- 出错了,请与管理员联系。。。错误提示页面-NullPointerException <br>
- <span th:text="${error}"></span>
- </body>
- </html>
复制代码
@ControllerAdvice+@ExceptionHandler处置处罚
第二种处置处罚方式中,非常处置处罚的代码和业务代码放在一个类中了,这种方式耦合性太强了,最好是将业务和非常处置处罚的代码分离开,这时我们可以定义一个专门的非常处置处罚类,通过注解@ControllerAdvice来实现。具体如下:
- @ControllerAdvice
- public class GlobalException {
- /**
- * java.lang.ArithmeticException
- * 该方法需要返回一个 ModelAndView:目的是可以让我们封装异常信息以及视图的指定
- * 参数 Exception e:会将产生异常对象注入到方法中
- */
- @ExceptionHandler(value={java.lang.ArithmeticException.class})
- public ModelAndView arithmeticExceptionHandler(Exception e){
- ModelAndView mv = new ModelAndView();
- mv.addObject("error", e.toString()+" -- advice");
- mv.setViewName("error1");
- return mv;
- }
- /**
- * java.lang.NullPointerException
- * 该方法需要返回一个 ModelAndView:目的是可以让我们封装异常信息以及视
- 图的指定
- * 参数 Exception e:会将产生异常对象注入到方法中
- */
- @ExceptionHandler(value={java.lang.NullPointerException.class})
- public ModelAndView nullPointerExceptionHandler(Exception e){
- ModelAndView mv = new ModelAndView();
- mv.addObject("error", e.toString()+" -- advice");
- mv.setViewName("error2");
- return mv;
- }
- }
复制代码 控制器中实现
- @Controller
- public class UserController {
- /**
- * 模拟 NullPointerException
- * @return
- */
- @RequestMapping("/show1")
- public String showInfo(){
- String str = null;
- str.length();
- return "index";
- }
- /**
- * 模拟 ArithmeticException
- * @return
- */
- @RequestMapping("/show2")
- public String showInfo2(){
- int a = 10/0;
- return "index";
- }
- }
复制代码
SimpleMappingExceptionResolver处置处罚
我们还可以通过SimpleMappingExceptionResolver将具体的非常和错误页面指定对应关系,如许就不消每个非常都单独写一个方法了。
- @Configuration
- public class GlobalException {
- /**
- * 该方法必须要有返回值。返回值类型必须是:
- SimpleMappingExceptionResolver
- */
- @Bean
- public SimpleMappingExceptionResolver
- getSimpleMappingExceptionResolver(){
- SimpleMappingExceptionResolver resolver = new
- SimpleMappingExceptionResolver();
- Properties mappings = new Properties();
- /**
- * 参数一:异常的类型,注意必须是异常类型的全名
- * 参数二:视图名称
- */
- mappings.put("java.lang.ArithmeticException", "error1");
- mappings.put("java.lang.NullPointerException","error2");
- //设置异常与视图映射信息的
- resolver.setExceptionMappings(mappings);
- return resolver;
- }
- }
复制代码 自定义HandlerExceptionResolver处置处罚
末了我们还可以通过实现HandlerExceptionResolver 接口来根据不同非常类型来动态处置处罚非常。
- @Configuration
- public class GlobalException implements HandlerExceptionResolver {
- /**
- * @param httpServletRequest
- * @param httpServletResponse
- * @param o
- * @param e
- * @return
- */
- @Override
- public ModelAndView resolveException(HttpServletRequest httpServletRequest
- , HttpServletResponse httpServletResponse
- , Object o, Exception ex) {
- ModelAndView mv = new ModelAndView();
- //判断不同异常类型,做不同视图跳转
- if (ex instanceof ArithmeticException) {
- mv.setViewName("error1");
- }
- if (ex instanceof NullPointerException) {
- mv.setViewName("error2");
- }
- mv.addObject("error", ex.toString());
- return mv;
- }
- }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |