Springboot学习06-自定义错误页面完整分析

前言

  接着上一篇博客,继续分析Springboot错误页面问题

正文

1-自定义浏览器错误页面(只要将自己的错误页面放在指定的路径下即可)

1-1-Springboot错误页面匹配机制(以404错误为例):

  • 1-在模板引擎下:找templates/error/404.html;如果没有,则继续匹配
  • 2-在模板引擎下:找templates/error/4XX.html;如果没有,则继续匹配
  • 3-在静态资源下:找static/error/404.html;如果没有,则继续匹配
  • 4-匹配最后的“围墙”:WhiteLevel Erro Page页面

1-2-补充说明

1-3-demo示例

1-4-简单自定义页面的缺陷

  • 1-只能展示Springboot默认的返回信息:timestamp时间戳;status状态;error错误提示;exception异常对象;message异常消息等简单返回信息;无法返回自定义业务数据

2-自定义错误的json

2-1-源码分析

//1-自定义Exception
public class DataException extends RuntimeException { public DataException() {
super("数据不存在!");
}
} //2-自定义handleException方法
@ControllerAdvice
public class MyExceptionHandler { @ResponseBody
@ExceptionHandler(DataException.class)
public Map<String,Object> handleException(Exception e){ Map<String,Object> map = new HashMap<String,Object>();
map.put("code","data error");
map.put("msg",e.getMessage()); return map; }
} //3-测试接口
@Controller
public class DemoController { @GetMapping(value="test")
public String toExceptionPage( ){
throw new DataException();
}
}

2-2-页面效果

2-3-缺点:浏览器请求也返回了json数据;不符合期望

3-自定义错误页面,自适应浏览器请求和客户端请求

3-1-源码示例

//1-自定义Exception
public class DataException extends RuntimeException { public DataException() {
super("数据不存在!");
}
} //2-自定义handleException方法
@ControllerAdvice
public class MyExceptionHandler { @ExceptionHandler(DataException.class)
public String handleException(Exception e, HttpServletRequest request){
//传入我们自己的错误状态码 4xx 5xx,否则就不会进入定制错误页面的解析流程
request.setAttribute("javax.servlet.error.status_code",500);
Map<String,Object> map = new HashMap<String,Object>();
map.put("code","data error");
map.put("msg",e.getMessage()); return "forward:/error"; }
} //3-测试接口
@Controller
public class DemoController { @GetMapping(value="test")
public String toExceptionPage( ){
throw new DataException();
}
}

3-2-demo示例

3-3-缺点:虽然已经兼容了浏览器请求和客户端请求;但是无法展示业务数据

4-自定义页面终版:自适应浏览器请求和客户端请求,并且允许返回业务数据

4-1-源码解析

//1-自定义ErrorAttributes
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {   //重写getErrorAttributes方法-添加自己的项目数据
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest,includeStackTrace); errorAttributes.put("myName","我不吃番茄");//自定义数据
errorAttributes.put("myAge","不告诉你");//自定义数据 return errorAttributes;
}
} //2-自定义Exception
public class DataException extends RuntimeException { public DataException() {
super("数据不存在!");
}
} //3-自定义handleException方法
@ControllerAdvice
public class MyExceptionHandler { @ExceptionHandler(DataException.class)
public String handleException(Exception e, HttpServletRequest request){
//传入我们自己的错误状态码 4xx 5xx,否则就不会进入定制错误页面的解析流程
request.setAttribute("javax.servlet.error.status_code",500);//这里只接受500状态错误
Map<String,Object> map = new HashMap<String,Object>();
map.put("code","data error");
map.put("msg",e.getMessage());
request.setAttribute("extra", map);//放在request中的数据,在前端页面中都可以取出来
return "forward:/error";//并不直接返回视图名称或json数据,请求转发到"/error",让Springboo按流程处理处理,从而达到自适应浏览器请求和客户端请求; }
} //4-测试接口
@Controller
public class DemoController { @GetMapping(value="test")
public String toExceptionPage( ){
throw new DataException();//主动抛出一个500错误,用于测试
}
}
//templates/error/5XX.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>自定义页面 --路径:templates/error/5XX.html页面 --优先级别:2</h1>
<h1>status:[[${status}]]</h1>
<h2>timestamp:[[${timestamp}]]</h2>
<h2>exception:[[${exception}]]</h2>
<h2>myName:[[${myName}]]</h2>
<h2>myAge:[[${myAge}]]</h2>
<h2>extra-code:[[${extra.code}]]</h2>
<h2>extra-msg:[[${extra.msg}]]</h2>
</body>
</html>

4-2-demo示例

Springboot学习05-自定义错误页面完整分析的更多相关文章

  1. Springboot学习04-默认错误页面加载机制源码分析

    Springboot学习04-默认错误页面加载机制源码分析 前沿 希望通过本文的学习,对错误页面的加载机制有这更神的理解 正文 1-Springboot错误页面展示 2-Springboot默认错误处 ...

  2. Springboot异常处理和自定义错误页面

    1.异常来源 要处理程序发生的异常,首先需要知道异常来自哪里? 1.前端错误的的请求路径,会使得程序发生4xx错误,最常见的就是404,Springboot默认当发生这种错误的请求路径,pc端响应的页 ...

  3. java web 自定义错误页面 完整jsp错误页面代码(同时写错误日志) error.jsp

    1.首先配置web.xml  添加一下代码 <error-page> <error-code>500</error-code> <location>/e ...

  4. SpringBoot自定义错误页面,SpringBoot 404、500错误提示页面

    SpringBoot自定义错误页面,SpringBoot 404.500错误提示页面 SpringBoot 4xx.html.5xx.html错误提示页面 ====================== ...

  5. springboot自定义错误页面

    springboot自定义错误页面 1.加入配置: @Bean public EmbeddedServletContainerCustomizer containerCustomizer() { re ...

  6. Springboot - 自定义错误页面

    Springboot 没找到页面或内部错误时,会访问默认错误页面.这节我们来自定义错误页面. 自定义错误页面 1.在resources 目录下面再建一个 resources 文件夹,里面建一个 err ...

  7. ASP.NET全局错误处理和异常日志记录以及IIS配置自定义错误页面

    应用场景和使用目的 很多时候,我们在访问页面的时候,由于程序异常.系统崩溃会导致出现黄页.在通常的情况下,黄页对于我们来说,帮助是极大的,因为它可以帮助我们知道问题根源,甚至是哪一行代码出现了错误.但 ...

  8. Spring Boot自定义错误页面,Whitelabel Error Page处理方式

    我已经是Spring Framework框架的忠实粉丝.对于企业软件开发者来说它提供了对常见问题的通用解决方案,包括那些你在未来开发中没有意识到的问题.但是,它构建的J2EE项目变得比较臃肿,需要被一 ...

  9. ASP.NET自定义错误页面

    ASP.NET自定义错误页面 ASP.NET 提供三种用于在出现错误时捕获和响应错误的主要方法:Page_Error 事件.Application_Error 事件以及应用程序配置文件 (Web.co ...

随机推荐

  1. spring aop无法拦截类内部的方法调用

    1.概念 拦截器的实现原理就是动态代理,实现AOP机制.Spring 的代理实现有两种:一是基于 JDK Dynamic Proxy 技术而实现的:二是基于 CGLIB 技术而实现的.如果目标对象实现 ...

  2. Hanlp中N最短路径分词详细介绍

    N-最短路径 是中科院分词工具NLPIR进行分词用到的一个重要算法,张华平.刘群老师在论文<基于N-最短路径方法的中文词语粗分模型>中做了比较详细的介绍.该算法算法基本思想很简单,就是给定 ...

  3. [转]阿里巴巴十年Java架构师分享,会了这个知识点的人都去BAT了

    1.源码分析专题 详细介绍源码中所用到的经典设计思想,看看大牛是如何写代码的,提升技术审美.提高核心竞争力. 帮助大家寻找分析源码的切入点,在思想上来一次巨大的升华.知其然,并知其所以然.把知识变成自 ...

  4. 阅读 video in to axi4-stream v4.0 笔记

    阅读 video in to axi4-stream v4.0 笔记 axi4 stream里面只传输的有效数据. 引用: 使能了video timing controller core 的所用信号, ...

  5. ant design + react,自动获取上传音频的时长(react-audio-player)

    在后台管理项目中,用户要求上传音频,并且自动获取音频时长. 第一步, import { Upload, Button, Icon } from 'antd'; 第二步,在表单中使用 Upload 组件 ...

  6. Python程序打包之PyInstaller

    1.背景说明 [Python版本]Python 2.7.14 [系统平台]Windows 7 [优缺点描述]据说PyInstaller 比较慢,但是PyInstaller打包出来的exe简洁(就一个文 ...

  7. NodeJs中类定义及类使用

    1.首先定义类Point,文件名为point.class.js: // 定义类 class Point { //构造函数 constructor(x, y) { this.x = x;//类中变量 t ...

  8. Scrapy学习篇(十一)之设置随机User-Agent

    大多数情况下,网站都会根据我们的请求头信息来区分你是不是一个爬虫程序,如果一旦识别出这是一个爬虫程序,很容易就会拒绝我们的请求,因此我们需要给我们的爬虫手动添加请求头信息,来模拟浏览器的行为,但是当我 ...

  9. Bootstrap 前端UI框架

    Bootstrap 有哪些优越性? 1.简单灵活的用于搭建WEB页面的HTML,CSS, JavaScript的工具集 2.基于html5, css3, 具有良好特性,友好的学习曲线,卓越的兼容性,1 ...

  10. 转apk打包

    常规打包方式: -------------------------------------------------------------------------------------------- ...