1. 自定义拦截器
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println(request.getRequestURI());
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
配置类
// WebMvcConfigurer的接口方法全部是default
@Configuration
public class MyConfigurer implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
MyInterceptor myInterceptor = new MyInterceptor();
// 需要拦截的路径,以及排出拦截的路径
registry.addInterceptor(myInterceptor).addPathPatterns("/**").excludePathPatterns("/resources/**");
}
}
2. 自定义页面跳转
访问 localhost:端口/contextName/index - 自动跳转到main.html页面
// WebMvcConfigurer的接口方法全部是default
@Configuration
public class MyConfigurer implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// addViewController:对应的地址栏请求
// setViewName:渲染页面的实际路径
registry.addViewController("index").setViewName("main");
}
}
3. 自定义静态资源的映射 - 可通过配置文件取代
静态资源配置 -可以访问static-locations子目录的文件
# 静态资源的路径
mvc:
# 路径匹配
static-path-pattern: /resources/**
resources:
# 实际映射的静态资源文件路径
static-locations: classpath:/resources/
上面的配置等价于下面的代码
代码配置 -不可以访问ddResourceLocations子目录的文件
@Configuration
public class MyConfigurer implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources123/**").addResourceLocations("classpath:resources/");
}
}
4. 自定义状态码页面
BookController控制器
@Controller
@RequestMapping("books")
public class BookController {
@GetMapping("exception1")
@ResponseBody
public String testException() {
throw new MyException("自定义错误1");
}
@PostMapping("exception2")
@ResponseBody
public String testException2() {
throw new MyException("自定义错误2");
}
}
5xx.html - thymeleaf可接收异常 - 获取异常的信息
<!DOCTYspanE html>
<html lang="en" xmlns:th=httspan://www.thymeleaf.org>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>5xx自定义状态码页面</h1>
timestamspan:<span th:text="${timestamspan}"></span>
<br>
status:<span th:text="${status}"></span>
<br>
error:<span th:text="${error}"></span>
<br>
excespantion:<span th:text="${excespantion}"></span>
<br>
message:<span th:text="${message}"></span>
<br>
errors:<span th:text="${errors}"></span>
<br>
</body>
</html>
1. 随便输入一个SpringBoot应用下的地址 - 没写接口 - 404页面
2. 接口地址正确,但请求方式不对,浏览器请求是Get,但我写的接口是Post请求 - 405页面
3. 接口地址正确,但请求方式正确,但执行过程中抛出异常 - 500页面
5. 自定义统一异常处理
代码步骤
- @RestControllerAdvice 声明是异常处理中心类
- @ExceptionHandler 声明处理的是什么类型的异常方法
Spring统一异常处理架构图
BookController控制器
@Controller
@RequestMapping("books")
public class BookController {
@GetMapping("exception1")
@ResponseBody
public String testException() {
throw new MyException("自定义错误1");
}
}
MyExceptionHandler统一异常处理类
@ControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(MyException.class)
public ModelAndView ExcpetionHandler(MyException myException) {
ModelAndView model = new ModelAndView();
System.out.println("我经过这里MyExceptionHandler");
model.addObject("message", myException.getMessage());
model.addObject("note", "this is @ControllerAdvice");
model.setViewName("error/5xx");
return model;
}
}
5xx.html状态码页面
<!DOCTYspanE html>
<html lang="en" xmlns:th=httspan://www.thymeleaf.org>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>5xx自定义状态码页面</h1>
message:<span th:text="${message}"></span>
<br>
note: <span th:text="${note}"></span>
<br>
</body>
</html>
测试