相关博客:SpringBoot 统一异常处理
第一步:创建项目
添加Maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 集成thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.yml
server:
port: 80
servlet:
context-path: /wpe
spring:
freemarker:
enabled: true #是否启用thymeleaf作为视图解析
cache: false # 缓存配置 开发阶段应该配置为false 因为经常会改
charset: UTF-8 # 文件编码
#前缀,也就是模板存放的路径
prefix: classpath:/templates/
suffix: .html # 模版后缀名 默认为ftl
#编码格式
encoding: UTF-8
#设置不严格的html
mode: HTML
servlet:
content-type: text/html
第二步:自定义异常
CustomException.java:
public class CustomException extends RuntimeException {
// 异常错误编码
private Integer code;
// 异常信息
private String msg;
private CustomException() {
}
public CustomException(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
ModelAndViewException.java
public class ModelAndViewException extends RuntimeException {
// 异常错误编码
private Integer code;
// 异常信息
private String msg;
private ModelAndViewException() {
}
public static ModelAndViewException transfer(CustomException e) {
return new ModelAndViewException(e.getCode(), e.getMessage());
}
private ModelAndViewException(int code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
第三步:自定义异常处理器
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(ModelAndViewException.class)
public ModelAndView viewExceptionHandler(HttpServletRequest req, ModelAndViewException e) {
ModelAndView modelAndView = new ModelAndView();
//将异常信息设置如modelAndView
modelAndView.addObject("exception", e);
modelAndView.addObject("url", req.getRequestURL());
modelAndView.setViewName("error");
//返回ModelAndView
return modelAndView;
}
}
第四步:自定义注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface ModelAndView {
}
第五步:创建Controller
@Controller
@RequestMapping("/mave")
@Slf4j
public class ModelAndViewExceptionController {
/**
* 模拟系统异常
*/
@ModelAndView
@GetMapping("/fun1")
public String fun1() {
try {
Class.forName("com.mysql.jdbc.xxxx.Driver");
} catch (ClassNotFoundException e) {
throw new CustomException(400,"在XXX业务,ff1()方法内,出现ClassNotFoundException");
}
return "index";
}
}
第六步:在resources/templates目录下创建error.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta charset="UTF-8"/>
<title>错误页面</title>
</head>
<body>
<h1>error</h1>
<div th:text="${exception}"></div>
<div th:text="${url}"></div>
</body>
</html>