# springboot-errorpage
**Repository Path**: stormlong/springboot-errorpage
## Basic Information
- **Project Name**: springboot-errorpage
- **Description**: 自定义错误页面
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2024-10-29
- **Last Updated**: 2024-10-31
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# 自定义错误页面
背景:当我们访问应用程序不存在的接口路径或者参数传递不规范时,springboot 默认提示 如下页面

该页面对用户不友好,我们可以自定义展示错误页来改善。
优化后的简洁效果,可对 html 页面进一步美化,这里只说明修改默认错误页方法。

## demo地址
https://gitee.com/stormlong/springboot-errorpage
## 官方文档
https://docs.spring.io/spring-boot/docs/2.7.18/reference/html/web.html#web.servlet.spring-mvc.error-handling.error-pages


## 静态页面
resources\public\error
不引入任何模板引擎时,在这个目录下寻找
且文件名和错误状态码保持一致
## 动态页面
resources\templates\error
引入模板引擎时,在这个目录下寻找
例 :
```xml
org.springframework.boot
spring-boot-starter-thymeleaf
```
且文件名和错误状态码保持一致
## 优先级
html 文件命名也可以为 4xx 或者是 5xx ,这里的 xx 代表了 404, 401等异常错误
不引入任何模板引擎时,优先具体的错误码页面,然后是 4xx页面,即
1. \public\error\404.html
2. \public\error\4xx.html
引入模板引擎时,优先级
1. \templates\error 目录下的 404.html
2. \public\error 目录下的 404.html
3. \templates\error 目录下的 4xx.html
4. \public\error 目录下的 4xx.html
没有任何错误页面时,默认来到 SpringBoot 默认的错误提示页面
总结:优先具体的错误码页面,然后动态目录下的 4xx 页面

## 原理解析
### 处理异常类
在spring boot中,我们可以找到 BasicErrorController,这个类主要用来处理异常
```java
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.boot.autoconfigure.web.servlet.error;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.error.ErrorAttributeOptions.Include;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
//如果没有配置server.error.path就去error.path找,如果没有配置默认路径为/error
@RequestMapping({"${server.error.path:${error.path:/error}}"})
public class BasicErrorController extends AbstractErrorController {
private final ErrorProperties errorProperties;
public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
this(errorAttributes, errorProperties, Collections.emptyList());
}
public BasicErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List errorViewResolvers) {
super(errorAttributes, errorViewResolvers);
Assert.notNull(errorProperties, "ErrorProperties must not be null");
this.errorProperties = errorProperties;
}
@RequestMapping(
produces = {"text/html"}
)
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
HttpStatus status = this.getStatus(request);
Map model = Collections.unmodifiableMap(this.getErrorAttributes(request, this.getErrorAttributeOptions(request, MediaType.TEXT_HTML)));
response.setStatus(status.value());
ModelAndView modelAndView = this.resolveErrorView(request, response, status, model);
return modelAndView != null ? modelAndView : new ModelAndView("error", model);
}
@RequestMapping
public ResponseEntity