1、总览

2、代码

1)、pom.xml

这里注意:springboot 2.2.0以后默认的freemarker文件后缀为:ftlh。本例用的是2.2.1,所以后缀为ftlh

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency> </dependencies>

2)、application.properties

#必须关闭whitelabel,否则无法导航到错误ftlh页面上
server.error.whitelabel.enabled=false
server.error.include-stacktrace=always

3)、java代码,与https://www.cnblogs.com/yaoyuan2/p/11873110.html一致

4)、5xx.ftlh

<!DOCTYPE html>
<html lang="en">
<head>
<style>
table td{
vertical-align:top;
border:solid 1px #888;
padding:10px;
}
</style>
</head>
<body>
<h1>My FreeMarker 5xx Error Page</h1>
<table>
<tr>
<td>Date</td>
<td>${timestamp?datetime}</td>
</tr>
<tr>
<td>Error</td>
<td>${error}</td>
</tr>
<tr>
<td>Status</td>
<td>${status}</td>
</tr>
<tr>
<td>Message</td>
<td>${message}</td>
</tr>
<tr>
<td>Exception</td>
<td>${exception!""}</td>
</tr>
<tr>
<td>Trace</td>
<td>
<pre>${trace}</pre>
</td>
</tr>
</table>
</body>
</html>

注意:在freemarker中${exception}是空的,导致出错,进而执行不到前模板页面了。因此,此处加上了!"",表示为空或找不到时为""。

5)、404.ftlh

<!DOCTYPE html>
<html lang="en">
<head>
<style>
table td{
vertical-align:top;
border:solid 1px #888;
padding:10px;
}
</style>
</head>
<body>
<h1>My FreeMarker 404 Error Page</h1>
<table>
<tr>
<td>Date</td>
<td>${timestamp?datetime}</td>
</tr>
<tr>
<td>Error</td>
<td>${error}</td>
</tr>
<tr>
<td>Status</td>
<td>${status}</td>
</tr>
<tr>
<td>Message</td>
<td>${message}</td>
</tr>
<tr>
<td>Exception</td>
<td>${exception!"No exception thrown"}</td>
</tr>
<tr>
<td>Trace</td>
<td>
<pre>${trace!"No Stacktrace available"}</pre>
</td>
</tr>
</table>
</body>
</html>

6)、error.ftlh

<!DOCTYPE html>
<html lang="en">
<head>
<style>
table td{
vertical-align:top;
border:solid 1px #888;
padding:10px;
}
</style>
</head>
<body>
<h1>My FreeMarker Custom Global Error Page</h1>
<table>
<tr>
<td>Date</td>
<td>${timestamp?datetime}</td>
</tr>
<tr>
<td>Error</td>
<td>${error}</td>
</tr>
<tr>
<td>Status</td>
<td>${status}</td>
</tr>
<tr>
<td>Message</td>
<td>${message}</td>
</tr>
<tr>
<td>Exception</td>
<td>${exception!"No exception"}</td>
</tr>
<tr>
<td>Trace</td>
<td>
<pre>${trace!"No trace"}</pre>
</td>
</tr>
</table>
</body>
</html>

3、执行

总结:

1、目录结构为:

templates

|_error.ftlh

|_error

  |_5xx.ftlh

  |_404.ftlh

2、application.properties中,必须加入:

server.error.whitelabel.enabled=false

如果想显示trace,也要加入:

server.error.include-stacktrace=always

springboot - 映射HTTP Response Status Codes 到 FreeMarker Error页面的更多相关文章

  1. springboot - 映射HTTP Response Status Codes 到 静态 HTML页面

    1.总览 2.代码 1).pom.xml <dependencies> <dependency> <groupId>org.springframework.boot ...

  2. springboot - 映射 HTTP Response Status Codes 到自定义 JSP Error 页面

    1.总览 2.代码 1).pom.xml <dependencies> <dependency> <groupId>org.springframework.boot ...

  3. HTTP状态码(HTTP Status codes)简介

    HTTP可能大家都熟悉,就是超文本传输协议.浏览器通过HTTP与WEB Server通讯(也有一些其它软件比如IM使用HTTP协议传递数据),把我们的请求(HTTP Request)传递给服务器,服务 ...

  4. HTTP response status

    The status code is a 3-digit number: 1xx (Informational): Request received, server is continuing the ...

  5. C#、JAVA操作Hadoop(HDFS、Map/Reduce)真实过程概述。组件、源码下载。无法解决:Response status code does not indicate success: 500。

    一.Hadoop环境配置概述 三台虚拟机,操作系统为:Ubuntu 16.04. Hadoop版本:2.7.2 NameNode:192.168.72.132 DataNode:192.168.72. ...

  6. IIS SMTP status codes

    Here are the meaning of SMTP status codes. Status Code Description 211 System status, or system help ...

  7. HTTP常见返回代码(HTTP Status codes)的分类和含义

    HTTP错误主要分成三类:用户设备问题.Web服务器问题和连接问题.当客户端向Web服务器发送一个HTTP请求时,服务器都会返回一个响应代码.而这些响应代码主要分成五类. HTTP状态码中定义了5大类 ...

  8. returned a response status of 403 OR 409

    当我们使用jersy把图片上传到我们的图片服务器中[tomcat],我们可能会有以下的错误: returned a response status of 403 OR 409 403和409我都遇到过 ...

  9. RobotFramework下的http接口自动化Get Response Status 关键字的使用

    Get Response Status 关键字用来获取http请求返回的http状态码. 示例1:访问苏宁易购网站上的http推荐接口,使用Get Response Status 关键字来获取返回的h ...

随机推荐

  1. 指定GPU运行python程序

    一.命令行运行python程序时 1.首先查看哪些GPU空闲,nvidia-smi显示当前GPU使用情况. nvidia-smi 2.然后指定空闲的GPU运行python程序. CUDA_VISIBL ...

  2. Day9 - D - Piggy-Bank POJ - 1384

    Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. ...

  3. redis缓存穿透,缓存击穿,缓存雪崩问题

    缓存穿透 缓存查询一般都是通过key去查找value,如果不存在对应的value,就要去数据库中查找.如果这个key对应的value是一定不存在的,并且对该key并发请求很大,就会对数据库产生很大的压 ...

  4. PostGIS官方教程汇总目录

    一.PostGIS介绍 二.PostGIS安装 三.创建空间数据库 四.加载空间数据 五.数据 六.简单的SQL语句 七.几何图形(Geometry) 八.关于几何图形的练习 九.空间关系 十.空间连 ...

  5. docker-compose 快速部署Prometheus,监控docker 容器, 宿主机,ceph -- cluster集群

    话不多说上菜: 现在环境是这样: ceph 4台: 192.168.100.21  ceph-node1 192.168.100.22  ceph-node2 192.168.100.23  ceph ...

  6. PCHMI工控组态开发视频教程

    PCHMI是一款适合所有PLC工程师快速上手工控组态开发的控件 下面是视频教程链接 PCHMI工控组态 02-按钮的使用 PCHMI工控组态 03-数据显示器使用 PCHMI工控组态 04-标签控件的 ...

  7. P1071 小赌怡情

    P1071 小赌怡情 转跳点:

  8. beanstalkd队列

    数据结构里面的队列是先进先出的数据结构,在服务器开发中使用队列可以有效的实现任务的异步处理,把耗时的任务放在一个队列中,由消费者去自动处理,比如客户端用户把错题生成pdf文件下载,可以在客户端点击生成 ...

  9. uni app中关于图片的分包加载

    因为在项目中使用了大量的静态资源图片,使得主包体积过大, 而把这些图片全部放到服务器又有点麻烦,就想能不能把图片也分包,但是直接放在分包下的话导致图片资源找不到了, 在社区中看到大佬分享的十分有用,特 ...

  10. 剑指offer 链表中环的入口位置

    题目描述 一个链表中包含环,请找出该链表的环的入口结点.   思路:这题需要知道a = c,然后head和slow每次走一步,相遇的时候就是第一个入口交点, 注意:for循环或者while循环之后,一 ...