一、SpringBoot默认错误页面简介
用ControllerAdvice和ExceptionHandler来自定义全局异常处理,一般用来处理应用级别异常,有一些容器级别异常就处理不了,例如Filter中抛出异常。
当发生404,500等请求错误时,SpringBoot默认的是由BasicErrorController类来处理,
其中errorHtml方法返回默认错误的HTML页面,error方法返回错误信息是HTML还是Json,要根据请求头的Accept参数。
二、错误页面简单配置
在resources/static目录下创建error目录,一种添加4xx.html,5xx.html,一种是添加具体的状态码命名文件,例如404.html
也可以在classpath:/templates/目录下创建error目录,一种添加4xx.html,5xx.html,一种是添加具体的状态码命名文件,例如404.html
SpringBoot默认返回5条错误相关的信息,分别是timestamp,status,error,message,path,可以添加到动态页面自定义处理展示
优先级:动态错误页面 > 静态错误页面
优先级:具体响应码文件 > 5xx.html 4xx.html
三、错误页面复杂配置
1.自定义Error数据
@Component
public class MyErrorAttribute extends DefaultErrorAttributes {
@Override
public Map getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
Map errorAttributes = super.getErrorAttributes(webRequest, options);
//放入自定义customMsg属性
errorAttributes.put("customMsg","自定义error属性");
//移除默认的error属性
errorAttributes.remove("error");
return errorAttributes;
}
}
当访问一个不存在的url,返回的json如下:
{
"timestamp": "2020-10-31T09:50:23.542+00:00",
"status": 404,
"message": "",
"path": "/error5",
"customMsg": "自定义error属性"
}
通过thymeleaf引擎,templates/error目录下配置404.html可以将这个值取出来
2.自定义Error视图
@Component
public class MyErrorViewResolver extends DefaultErrorViewResolver {
@Autowired
public MyErrorViewResolver(ApplicationContext applicationContext, ResourceProperties resourceProperties) {
super(applicationContext, resourceProperties);
}
@Override
public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map model) {
ModelAndView modelAndView = super.resolveErrorView(request, status, model);
//添加错误视图
modelAndView.setViewName("errorPage");
//model中添加数据
modelAndView.addObject("customMsg","出错了");
modelAndView.addAllObjects(model);
return modelAndView;
}
}
当访问一个不存在的url,返回的json如下:
{
"timestamp": "2020-10-31T09:55:58.004+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/error5"
}
//没有自定义错误属性
通过thymeleaf引擎,在templates/errorPage.html中也可以将这个值取出来
3.完全自定义Error视图和Error数据
@Controller
public class MyErrorController extends BasicErrorController {
//由于BasicErrorController没有无参构造,用@Autowired传递参数,自动导入
//ErrorProperties在启动类中注入
@Autowired
public MyErrorController(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List errorViewResolvers) {
super(errorAttributes, errorProperties, errorViewResolvers);
}
//重写errorHtml方法,自定义错误视图
@Override
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
ModelAndView modelAndView = super.errorHtml(request, response);
//添加错误视图
modelAndView.setViewName("errorPage");
//添加model属性
modelAndView.addObject("customMsg","出错了");
return modelAndView;
}
//重写error方法,返回自定义json
@Override
public ResponseEntity> error(HttpServletRequest request) {
ResponseEntity> responseEntity = super.error(request);
Map body = responseEntity.getBody();
//添加错误信息,json返回
body.put("hello", "world");
return responseEntity;
}
}
//在启动类中配置:
@Bean
public ErrorProperties errorProperties(){
return new ErrorProperties();
}
当访问一个不存在的url,返回的json如下:
{
"timestamp": "2020-10-31T10:01:36.088+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/error5",
"hello": "world"
}
通过thymeleaf,在templates/errorPage.html中也可以将这个值取出来
自定义hello未加入model中,而是通过json返回,thymeleaf提取不出来