MatrixRhythm

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

知识点:

  1、SpringBoot2.x服务端异常和SpringBoot配置全局异常

    1)、默认异常测试 int i = 1/0,不友好
    2)、异常注解介绍
    @ControllerAdvice 如果是返回json数据 则用 RestControllerAdvice,就可以不加 @ResponseBody

    //捕获全局异常,处理所有不可知的异常
    @ExceptionHandler(value=Exception.class)

  2、SpringBoot2.x配置全局异常返回自定义异常和错误页面跳转

    1)、返回自定义异常界面,需要引入thymeleaf依赖
  

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

    2)、resource目录下新建templates,并新建error.html

ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("error.html");
modelAndView.addObject("msg", e.getMessage());
return modelAndView;

     https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-error-handling

posted on 2019-06-01 22:47  Matrix锅锅  阅读(300)  评论(0编辑  收藏  举报