SpringBoot 2.0 配置错误页面

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

springboot 2.0 配置错误页面

@Configuration
public class ErrorPageConfig implements ErrorPageRegistrar {

	@Override
	public void registerErrorPages(ErrorPageRegistry registry) {
		ErrorPage error404 = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
		ErrorPage error500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
		registry.addErrorPages(error404, error500);
	}
}

注意错误页面是作为静态页面存在的, 所以静态页面要放在 /src/main/resources/static/ 目录下

如果是想要通过动态请求返回页面, 可以使用 ErrorPage 构造器参数使用对应的请求

其实, SpringBoot 默认的错误页面是在 /resources/static/error/ 目录下的 404.html, 500.html

如果使用了 Thymeleaf 的话, 错误页面是在 /resources/thymeleaf/error/ 目录下的 404.html, 500.html, 4xx.html, 5xx.html

转载于:https://my.oschina.net/zdtdtel/blog/3019203

你可能感兴趣的:(SpringBoot 2.0 配置错误页面)