错误处理机制:

访问一个不存在的页面时,或者程序抛出异常时

默认效果

  • 浏览器返回一个错误的页面,注意查看浏览器发送请求的请求头

  • 可以使用专业的软件比如postman分析返回的json数据

springboot错误处理的自动配置信息

主要给日容器中注册了以下组件:

  • ErrorPageCustomizer 系统出现错误以后来到error请求进行处理;相当于(web.xml注册的错误页面规则)
  • BasicErrorController 处理/error请求
  • DefaultErrorViewResolver 默认的错误视图解析器
  • DefaultErrorAttributes 错误信息
  • defaultErrorView 默认错误视图

@getErrorAttributes()返回的参数

  • timestamp:时间戳
  • status:状态码
  • error:错误提示
  • exception:异常对象
  • message:异常消息
  • errors:JSR303数据校验的错误都在这里

2.0以后默认是不显示exception的,需要在配置文件中开启

server.error.include-exception=true

如何定制JSON数据

springboot做了自适应效果,浏览器访问响应错误页面。客户端访问响应错误信息的json数据

  1. 第一种方法,定义全局异常处理器类注入到容器中,捕获到异常返回json格式的数据

    @ControllerAdvice
    public class MyExceptionHandler {@ResponseBody@ExceptionHandler(Exception.class)public Map<String, Object> handleException(Exception e) {Map<String, Object> map = new HashMap(2);map.put("code", "100011");map.put("msg", e.getMessage());return map;}
    }
  2. 由上面我们已经知道数据的来源是调用DefaultErrorAttributes的getErrorAttributes方法得到的,而这个DefaultErrorAttributes是在ErrorMvcAutoConfiguration配置类中注册的,并且注册之前会检查容器中是否已经拥有
        @Bean@ConditionalOnMissingBean(value = {ErrorAttributes.class},search = SearchStrategy.CURRENT)public DefaultErrorAttributes errorAttributes() {return new DefaultErrorAttributes(this.serverProperties.getError().isIncludeException());}

所以我们可以只要实现ErrorAttributes接口或者继承DefaultErrorAttrites类,然后注册到容器中就行了

@ControllerAdvice
public class MyExceptionHandler {@ExceptionHandler(Exception.class)public String handleException(Exception e, HttpServletRequest request) {Map<String, Object> map = new HashMap(2);map.put("name", "hello");map.put("password", "123456");//设置状态码request.setAttribute("javax.servlet.error.status_code", 500);//把数据放到request域中request.setAttribute("ext", map);return "forward:/error";}
}
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {@Beanpublic DefaultErrorAttributes errorAttributes() {return new MyErrorAttributes();}class MyErrorAttributes extends DefaultErrorAttributes {@Overridepublic Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {//调用父类的方法获取默认的数据Map<String, Object> map = new HashMap<>(super.getErrorAttributes(webRequest, includeStackTrace));//从request域从获取到自定义数据Map<String, Object> ext = (Map<String, Object>) webRequest.getAttribute("ext", RequestAttributes.SCOPE_REQUEST);map.putAll(ext);return map;}}......

SpringBoot默认的错误处理机制相关推荐

  1. SpringBoot之错误处理机制

    文章目录 1.SpringBoot默认的错误处理机制 2.错误处理原理 (1)DefaultErrorAttributes (2)BasicErrorController:处理默认的/error请求 ...

  2. Springboot 错误处理机制

    SpringBoot默认的错误处理机制 即我们常见的白色的ErrorPage页面 浏览器发送的请求头: 如果是其他的请求方式,比如客户端,则相应一个json数据: 原理:是通过 ErrorMvcAut ...

  3. springboot返回modelandview 找不到视图_SpringBoot错误处理机制及原理

    SpringBoot错误信息处理机制 ★ 在一个web项目中,总需要对一些错误进行界面或者json数据返回,已实现更好的用户体验,SpringBoot中提供了对于错误处理的自动配置 " Er ...

  4. SpringBoot——错误处理机制 定制错误页面 (源码分析)

    目录 一.错误处理机制 二.ErrorPageCustomizer 三.BasicErrorController 四.DefaultErrorViewResolver 五.如何定制错误响应页面 六.D ...

  5. SpringBoot之自定义错误页面

     SpringBoot 默认配置: 如果是浏览器请求URL错误,返回错误页面:如果是postman等非浏览器客户端请求URL错误,返回 json 数据: SpringBoot源码: @Controll ...

  6. SpringBoot默认包扫描机制及@ComponentScan指定扫描路径详解

    SpringBoot默认包扫描机制及@ComponentScan指定扫描路径详解 SpringBoot默认包扫描机制 标注了@Component和@Component的衍生注解如@Controller ...

  7. SpringBoot默认包扫描机制及使用@ComponentScan指定扫描路径

    SpringBoot默认包扫描机制 标注了@Component和@Component的衍生注解如@Controller,@Service,@Repository就可以把当前的Bean加入到IOC容器中 ...

  8. SpringBoot异常处理-自定义错误页面

    自定义错误页面 SpringBoot 默认的处理异常的机制:SpringBoot 默认的已经提供了一套处理异常的机制. 一旦程序中出现了异常SpringBoot 会像/error 的url 发送请求. ...

  9. 学习SpringBoot笔记以及错误记录

    学习SpringBoot笔记以及错误记录 <1>. 启动类(SpringBootApplication)放错位置 错误场景:MainApplication类不应放在默认的src.main. ...

最新文章

  1. 45个超实用的JavaScript技巧及最佳实践(一)
  2. 叶杰平入选、华人占4成,2020年ACM杰出科学家榜单出炉
  3. 【LOJ】#2014. 「SCOI2016」萌萌哒
  4. STRUTS模拟试题
  5. php获取当前周得周一_PHP怎样获得最近一个周一和上周一的日期?
  6. php转化IP为整形
  7. 一个人的旅行 图论最短路问题
  8. 财务有必要学python吗-财务人是否应该学Python?
  9. 可道云访问服务器其他文件,可道云怎么用_可道云使用功能解析
  10. deepspeech 1 (百度 2014 论文解读)
  11. 计算机网络管理工程师证书考试试题,网络工程师考试模拟试题四-试题试卷网...
  12. 【快乐离散数学】谓词与量词 | 嵌套量词 | 狄摩根定律 | Predicates and Quantifiers | Nested Quantifiers
  13. asp.net(c#)中IsPostBack是什么意思
  14. metinfo 5.3.19密码重置漏洞
  15. 你真的了解“药品追溯码”吗?
  16. 女友忽然欲买文胸,但不知何色更美,Python解之【全程实战,议藏之】
  17. 【养生】【作息】作息+十二时辰养生
  18. Coding沙龙之vbs脚本
  19. Cannot resolve reference to bean sqlSessionFactory while setting bean property sqlSessionFactory
  20. 计算机 拔电源 重启,电脑在关机就自动重新启动。但拔掉电源插头再关就又不会重新启动了。求高手帮忙!!!...

热门文章

  1. 如何升级浏览器_手把手教你申请IOS14 Beta升级方法
  2. Java学习笔记—UDP通信
  3. 【转】VTK修炼之道2_VTK体系结构1
  4. 【转】Jenkins详细教程
  5. GetType和typeof的区别
  6. 由浅到深理解ROS URDF教程
  7. Angular 下拉搜索框实现
  8. CCIE-LAB-第三篇-VRRP+SLA
  9. 【机器学习】 - 关于图像质量评价IQA(Image Quality Assessment)
  10. 【HDU - 2104】hide handkerchief (素数)