springboot配置错误页面
主要是解决以下问题:
- 用户输入不存在的url;必需参数未传
- 服务器异常
解决方法:
- 在templates文件夹下,直接创建error页面,这样所有异常将直接转到这个页面
- 在templates文件夹下,直接创建error文件夹。在error文件夹里,创建404.html,500.html 这种以http错误值为名称的页面。此时,当产生某种错误时,将直接跳到相应页面。
同时使用时,以2为准
分析:
- 先看ErrorMvcAutoConfiguration,由注解可知,当注册dispatcherServlet加载时,会自动加载这个类。
- 当没有类实现ErrorController接口时,会注册一个BasicErrorController。所以,如果不想使用springboot提供的默认配置,也可以自己实现ErrorController接口,继承后默认配置不再生效
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({Servlet.class, DispatcherServlet.class})
@AutoConfigureBefore({WebMvcAutoConfiguration.class})
@EnableConfigurationProperties({ResourceProperties.class})
public class ErrorMvcAutoConfiguration {@Bean@ConditionalOnMissingBean(value = {ErrorController.class},search = SearchStrategy.CURRENT)public BasicErrorController basicErrorController(ErrorAttributes errorAttributes) {return new BasicErrorController(errorAttributes, this.serverProperties.getError(), this.errorViewResolvers);}
- BasicController默认的url是“error”,修改配置文件的server.error.path可以修改。
@RequestMapping(produces = "text/html")public ModelAndView errorHtml(HttpServletRequest request,HttpServletResponse response) {HttpStatus status = getStatus(request);Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));response.setStatus(status.value());ModelAndView modelAndView = resolveErrorView(request, response, status, model);return (modelAndView != null) ? modelAndView : new ModelAndView("error", model);}
- resolveErrorView方法在父类中,此方法主要用于根据http错误码返回相应的modelAndView
protected ModelAndView resolveErrorView(HttpServletRequest request,HttpServletResponse response, HttpStatus status, Map<String, Object> model) {for (ErrorViewResolver resolver : this.errorViewResolvers) {ModelAndView modelAndView = resolver.resolveErrorView(request, status, model);if (modelAndView != null) {return modelAndView;}}return null;}
- resolverErrorView方法在DefaultErrorViewResolver中
@Overridepublic ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status,Map<String, Object> model) {ModelAndView modelAndView = resolve(String.valueOf(status), model);if (modelAndView == null && SERIES_VIEWS.containsKey(status.series())) {modelAndView = resolve(SERIES_VIEWS.get(status.series()), model);}return modelAndView;}
- resolve
private ModelAndView resolve(String viewName, Map<String, Object> model) {String errorViewName = "error/" + viewName;TemplateAvailabilityProvider provider = this.templateAvailabilityProviders.getProvider(errorViewName, this.applicationContext);if (provider != null) {return new ModelAndView(errorViewName, model);}return resolveResource(errorViewName, model);}
由此方法可知404错误,直接对应/error/404页面。
由于我使用的是thymeleaf,所以provider并不是空。如果项目中没有使用jsp,thymleaf,groovy,freemarker等引擎模板,会导致provider未空,则不能返回相应的错误页面。
springboot配置错误页面相关推荐
- springboot系列六、springboot配置错误页面及全局异常
springboot系列六.springboot配置错误页面及全局异常 参考文章: (1)springboot系列六.springboot配置错误页面及全局异常 (2)https://www.cnbl ...
- springboot 配置错误页面
当系统出现问题时,为用户提供一个友好的页面,配置错误页是必不可少的 文章目录 一.编写配置类 二.控制层 三.前端页面 测试 一.编写配置类 @Configuration public class E ...
- SpringBoot 多种方式配置错误页面
参考资料 SpringBoot异常处理机制-BasicErrorController与@ControllerAdvice Java开发从工作到原理–BasicErrorController统一异常处理 ...
- ASP.NET配置错误页面浅析
ASP.NET配置错误页面在网站的运行过程中可能会面临很多问题,例如服务器出错,用户请求的页面不存在,程序配置错误等等一系列的问题.在用户请求浏览网页碰到这些的时候会出现一些系统默认的错误页面,这样对 ...
- web.xml配置错误页面
Why? 一般在我们程序员看来,出错了,我们因该需要看到错误信息,来让我们知道错误原因是什么,例如: 但是,对于用户群体来说,他们并不需要了解具体是发生了什么错误,只需要在报错的时候,提供给他们一个友 ...
- Springboot关于错误页面处理和统一异常处理
01.概述 在项目访问的时候我们经常会发生错误或者页面找不到,比如:资源找不到404,服务器500错误,默认情况下springboot的处理机制都是去跳转内部的错误地址:/error 和与之对应的一个 ...
- web.xml配置错误页面,及输出错误信息
转自:https://blog.csdn.net/bao19901210/article/details/23370407 1.需要在web.xml中配置相关信息 1 <!-- 默认的错误处理页 ...
- Servlet配置错误处理页面/配置错误页面
写一个错误处理页面 error.html <html> <head> <meta http-equiv="Content-Type" content= ...
- springboot 配置404页面
Thymeleaf模板自定义静态error页面方法 在resource/templates/error下添加error.html页面(error替换成对应的错误码,404.401.500等,还可以用4 ...
- nginx配置错误页面,处理tomat版本号泄露问题
一.问题描述 项目做安全扫描时,Tomcat报错页面泄漏Apache Tomcat/7.0.52相关版本号信息,安全策略要求去掉版本号信息. 二.解决方法 网上搜了下,大多都是简单粗暴的处理,直接去掉 ...
最新文章
- html文件修改后缀为aspx之后在ie6中格式显示不正确问题的解决办法
- 为了故意刁难AI,科学家们制造了这1200个问题,超强AI被“打回原形”
- Servlet--02--xml文件配置
- IDEA直接跳转到方法的实现类快捷键
- linuxoracle静默安装应答文件修改_Oracle 19c的examples静默安装
- 使用dotnet Cli向nuget发布包
- 程序员修仙之路--高性能排序多个文件
- Codeforce 189B——Counting Rhombi
- java 某年某月中第几周 开始时间和结束时间_重磅!库里又要签下一超级大合同!4年2亿啊!退役时间也定了...
- JS实时检测文本框内容长度
- 用牛顿法求方程的根的c语言编程,用牛顿迭代法和二分法求方程的根【C语言】...
- Python模块 - itertools循环器模块
- Python基础15_装饰器
- 哪有没时间这回事-读后感
- cell flash cache 的使用
- ecshop首页调用团购信息产品购买人数
- 微信小程序 去掉 微信昵称 emoji表情
- 2022年嵌入式系统设计师考试大纲
- 【软件新讯】VRay Next (4.0) for Rhino 正式发布与核心功能预览
- 小提琴弓是如何出现的?