知识点:

  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

相关文章:

  • 2021-06-22
  • 2021-12-07
  • 2021-09-15
  • 2021-11-20
  • 2021-12-28
  • 2021-08-06
  • 2021-11-19
猜你喜欢
  • 2022-12-23
  • 2021-11-29
  • 2022-12-23
  • 2021-11-29
  • 2021-04-26
  • 2021-11-29
  • 2021-11-19
相关资源
相似解决方案