- SpringBoot定制错误页面
- SpringBoot默认是将所有的错误请求转向/error,在类BasicErrorController可以看到
@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {
private final ErrorProperties errorProperties;
- 错误页面存放位置
html静态页面:在resources/public/error/ 下定义
如添加404页面: resources/public/error/404.html页面,中文注意页面编码
模板引擎页面:在templates/error/下定义
如添加5xx页面: templates/error/5xx.ftl templates/error/5xx.html
注:templates/error/ 这个的优先级比较 resources/public/error/高
- 以下演示:
- 新建SpringBoot项目添加web和thymeleaf组件
- 新建HelloController类
//直接抛出异常,那么浏览器是便会访问错误页面
@RequestMapping("/say6")
public String error(Model model){
throw new RuntimeException();
}
- 启动容器,访问/say6,此时后台报错且浏览器跳转到SpringBoot的默认错误页面,打印相关信息
- 新建resources/public/error/404.html 4xx.html 5xx.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
55555555555 5xx.html
</body>
</html>
- 启动容器,此时访问的是我们放在error目录下的错误页面。
- 可以在错误页面中打印如下错误信息:
timestamp:时间戳
status:状态码
error:错误提示
exception:异常对象
message:异常消息
errors:JSR303数据校验的错误都在这里
- 修改后的5xx.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<span th:text="'錯誤信息碼='+${status}"></span>
<span th:text="'錯誤信息='+${error}"></span>
<span th:text="'異常信息='+${exception}"></span>
<span th:text="'錯誤信息提示='+${message}"></span>
</body>
</html>
- SpringBoot定制错误数据
步骤1:自定义的Exception类继承自RumtimeException或者其他Exception类
public class MyException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String message;
private String str;
public MyException(String message,String str){
super("自定义异常");
this.message=message;
this.str=str;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
此处的message属性会自动的
步骤2:创建ExceptionHangder类处理controller类抛出的异常
@ControllerAdvice //表示這個類是用於處理其他controller類拋出的異常
public class MyExceptionHandler {
@ExceptionHandler(MyException.class)
public String handleException(Exception e, HttpServletRequest request){
Map<String,Object> map = new HashMap<>();
//传入我们自己的错误状态码 4xx 5xx
//Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
//设置成自己的状态码
request.setAttribute("javax.servlet.error.status_code",500);
map.put("handleException-message","handleException-message中存放的信息");
request.setAttribute("myMessageMap",map);
//转发到 /error由我们的BasicError 来处理返回JSON还是页面..
return "forward:/error";
}
}
步骤3:创建errorAttributes类将自定义的数据设置为可以在前端获取的
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
Map<String, Object> map = super.getErrorAttributes(requestAttributes, includeStackTrace);
map.put("getErrorAttributes-message", "getErrorAttributes-message中存放的信息");
//我们的异常处理器携带的数据 requestAttributes为请求域中取数据
Map<String,Object> myMessageMap = (Map<String, Object>) requestAttributes.getAttribute("myMessageMap", 0);
map.put("myMessageMap",myMessageMap);
return map;
}
}
步骤四:编写错误页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
<span th:text="'错误编码='+${status}"></span><br/>
<span th:text="'错误信息='+${error}"></span><br/>
<span th:text="'异常信息='+${exception}"></span><br/>
<span th:text="'message1错误信息提示='+${message1}"></span><br/>
<span th:text="'message2错误信息提示='+${message2}"></span><br/>
<span th:text="'xxx错误信息提示='+${xxx}"></span><br/>
<span th:text="'错误信息提示='+${company}"></span><br/>
<span th:text="'错误信息提示='+${message}"></span><br/>
</body>
</html>
步骤5:访问请求
@RequestMapping("/say6")
public String error(Model model){
throw new MyException("myteeeee","ddddddddd");
}
此处 throw new MyException(“myteeeee”,”ddddddddd”);其中的message属性被自动的返回到页面中去,即可用${message}获取
MyExceptionHandler类中new了一个Map,在MyErrorAttributes通过requestAttributes.getAttribute
(“myMessageMap”, 0);获取到该Map;
所有在前端可以获取到的参数值都是从
Map<String, Object> map =super.getErrorAttributes(requestAttributes, includeStackTrace);
的Map中获取到的。