作者Gitee地址 https://gitee.com/thciweicloud
作者项目 面包博客,一个微服务架构的前后端分离博客系统。
错误信息
- 文件展示顺序
在static、templates中创建error文件夹
精确的高于模糊的,动态的高于静态的,也就是templates的静态资源优先于static被展示
- 默认错误信息展示
yml配置
server:
error:
include-message: always
package com.thciwei.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
int i = 1 / 0;
return "hello";
}
}
5xx.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr>
<td>path</td>
<td th:text="${path}"></td>
</tr>
<tr>
<td>message</td>
<td th:text="${message}"></td>
</tr>
<tr>
<td>error</td>
<td th:text="${error}"></td>
</tr>
<tr>
<td>time</td>
<td th:text="${timestamp}"></td>
</tr>
<tr>
<td>status</td>
<td th:text="${status}"></td>
</tr>
</table>
</body>
</html>
path | /hello |
---|---|
message | / by zero |
error | Internal Server Error |
time | Thu Feb 11 09:30:01 CST 2021 |
status | 500 |
- 自定义异常数据
MyErrorAttribute
package com.thciwei.demo;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;
import java.util.Map;
@Component
public class MyErrorAttribute extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
Map<String, Object> map = super.getErrorAttributes(webRequest, includeStackTrace);
map.put("myerror", "这是我自定义的异常信息!");
return map;
}
}
<tr>
<td>myerror</td>
<td th:text="${myerror}"></td>
</tr>
myerror | 这是我自定义的异常信息! |
注册拦截器 springboot_filter
参数类型转换springboot_filter