html自定义错误页面,SpringBoot自定义错误页面

本文介绍了如何在SpringBoot中自定义错误页面和全局异常处理。通过创建`ControllerAdvice`和`ExceptionHandler`可以处理应用级别的异常,但无法处理容器级别的异常,如Filter中抛出的异常。SpringBoot默认使用`BasicErrorController`处理404、500等错误。你可以通过在`static`或`templates`目录下创建`error`目录并添加状态码对应的HTML文件来自定义错误页面。此外,还可以通过创建`ErrorAttribute`、`ErrorViewResolver`或自定义`ErrorController`来进一步定制错误数据和视图。文章展示了如何添加自定义属性、修改视图名称以及返回自定义JSON响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、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提取不出来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值