知识点:
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
原文地址:https://www.cnblogs.com/aaronRhythm/p/10961336.html
时间: 2024-10-28 10:05:12