首页 > 其他分享 >Spring Boot 错误和异常处理

Spring Boot 错误和异常处理

时间:2023-05-30 12:22:04浏览次数:29  
标签:错误 Spring Boot html error DispatcherServlet BasicErrorController

在 real time application 开发中,我们使用 Exception handling concept,以平滑终止程序。 它将 system error messages 转换为 user friendly error messages 。 但是在 Spring Boot 框架中,他们已经实现了 Exception handling concept ,这里我们需要了解 Spring Boot 框架内部如何处理 exceptions  和 Errors 。

Work flow

下图显示了 Spring Boot 中的 Error flow

在 spring / Spring boot 中使用 ErrorController 处理 errors,它将实现 BasicErrorController ,它提供了两种 methods

  1. errorHtml(): ModelAndView(从 browser 调用 trigger )
  2. error(): ResponseEntity<T> (从 non-browser/client app/postman /Any rest clients 等调用 trigger )

Work flow on Success:

Client -> Request -> DispatcherServlet -> doDispatch(req,resp) -> HandlerMapping -> Controller-> DispatcherServlet -> ViewResolver -> View -> Model (read) -> DispatcherServlet-> UI

Work flow on failure:

Client -> Request -> DispatcherServlet -> doDispatch(req,resp) -> HandlerMapping -> Controller-> DispatcherServlet -> BasicErrorController -> Response

ErrorAttributes

DefaultErrorAttributes:

BasicErrorController 将所有 error 详细信息收集为 Map<String,Object>

其中包含“ status,error, path, timestamp ...”等详细信息。此 ErrorMap 仅称为 Error Attributes ,作为对客户端的 final response 给出。

在spring boot中,如果你想自定义错误页面 white label error ,我们可以这样做。

Custom Error pages:  我们可以在“templates”文件夹下创建error.html文件,并根据需要进行设计。 这会在任何问题上执行(Http 状态:4xx 和 5xx)。 即 400,401,403,404…500,501…只执行一页即:error.html

使用 Errorcode.html(例如:404.html、500.html..)在 error folder 下创建特定的 error page 。 如果给定的 code 不匹配,它将转到 error.html。

Custom ErrorController:我们可以自定义默认的 ErrorController 代码。 Spring Boot 提供了 BasicErrorController 。 如果我们为 ErrorController 定义 impl,那么 Spring boot 会选择我们的类作为priority ,并且 error.html 、 404.html 和 5xx.html 也将不起作用。

Using Error Attributes in Custom Controller:

默认情况下,Spring Boot 为 error  提供了一些 attribute ,如 timestamp, status, path , message, exception, trace..etc

  • 要在我们的 custom error controller 中使用它们,只需自动装配该 property 即可。
@Autowired
private ErrorAttributes errorAttributes;
  • 读取所有具有当前请求 value 的 properties 。
ServletWebRequest webRequest = new ServletWebRequest(req);

 

标签:错误,Spring,Boot,html,error,DispatcherServlet,BasicErrorController
From: https://www.cnblogs.com/imreW/p/17442909.html

相关文章

  • SpringBoot集成RocketMQ,rocketmq_client.log日志文件配置
    SpringBoot项目集成rocketmq-client<dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-client</artifactId><version>4.7.0</version></dependency>项目启动时会在${user.home}/logs目录下创建一个roc......
  • SpringMVC
    本次文章是我的springmvc学习的笔记,是观看尚硅谷的视频,欢迎大家去学习观看:https://www.bilibili.com/video/BV1Ry4y1574R/?vd_source=f38047c43021f07cf7a4e84b564bde02一、SpringMVC简介1.1什么是MVCMVC是一种软件架构的思想,将软件按照模型、视图、控制器来划分M:Model,模......
  • 非常不错的springboot启动shell脚本
    #!/bin/bashJAVA_OPTIONS_INITIAL=-Xms256MJAVA_OPTIONS_MAX=-Xmx256M_JAR_KEYWORDS=/wls/appsystem/ruoyi/apps/ruoyi-admin.jarAPP_NAME=ruoyi-adminAPPLICATION_FILE=/wls/appsystem/ruoyi/config/application.propertiesAPPLICATION_YAML_FILE=/wls/appsystem/ruoyi......
  • SpringSecurity集成启动报 In the composition of all global method configuration,
    一.异常内容Causedby:org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'methodSecurityMetadataSource'definedinclasspathresource[org/springframework/security/config/annotation/method/configuration/GlobalMet......
  • An attempt has been made to start a new process before the current process has f
     Traceback(mostrecentcalllast): File"<string>",line1,in<module> File"E:\Eprogramfiles\Anaconda3\lib\multiprocessing\spawn.py",line116,inspawn_main   exitcode=_main(fd,parent_sentinel) File"E......
  • Spring+SpringMVC19_Spring练习-用户管理操作3
    一、用户列表展示1   二、用户列表展示2  三、用户添加操作-添加页面展示  四、用户添加操作-添加数据到数据库  五、用户添加操作-添加数据到数据库2  六、删除用户操作    ......
  • SpringBoot i18n 配置
    概述由于项目需要进行国际化提示,所以做了一下SpringBoot的i18n的配置。有两种方式实现i18n的配置操作页面每次访问后端都携带用户设定的语言类型的i18n国际化操作。在请求URL中设置一个字段?language=zh_CN的方式实现或者请求头中设置表明语言类型都行Accept-Language:......
  • 基于JAVA的springboot+vue医院信息管理系统、医院挂号管理系统,附源码+数据库+论文+PPT
    1、项目介绍任何系统都要遵循系统设计的基本流程,本系统也不例外,同样需要经过市场调研,需求分析,概要设计,详细设计,编码,测试这些步骤,基于java语言设计并实现了医院信管系统。该系统基于B/S即所谓浏览器/服务器模式,应用java技术,选择MySQL作为后台数据库。系统主要包括首页,个人中心,用户......
  • springboot集成themeleaf报Namespace 'th' is not bound问题的解决
    问题描述在我们想要在html前端页面使用th:符号时,发现他一直报错问题解决在html标签的最上方,也就是这里:加上这样一句代码:(加在html标签里面!!!)xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"这样就能够解决这个问题啦!......
  • Spring
    一、Spring简介优点:简化开发框架整合官网:https://spring.io发展史1.SpringFramework系统架构图2.目前问题代码书写现状耦合度偏高解决方案使用对象时,在程序中不要主动使用new产生对象,转换为由外部提供对象3.核心概念IOC(InversionofControl)控制......