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. 【转帖】IBM发布全新闪存解决方案,助企业存储架构做减法

    IBM发布全新闪存解决方案,助企业存储架构做减法 http://stor-age.zhiding.cn/stor-age/2020/0227/3124851.shtml 感觉 配图弄错了 FlashS ...

  2. 「Luogu P3820 小D的地下温泉」

    这道题的考点比较多. 前置芝士 BFS(DFS),这两种算法在这道题中并没有什么特别突出的地方,基本就是自己看心情写(本文以DFS为准,所以我心情是好是坏呢?) 连通块,可以将每一个温泉看作一个连通块 ...

  3. redis api-String

  4. Tomcat能启动,无法访问方法,

    好像没有扫描到controller 好像配置文件都没有加载成功 项目启动后,目录下多出一个ssmtest.xml文件 D:\Program Files\JDK-tomcat\apache-tomcat ...

  5. 「BJWC2012」冻结

    传送门 Luogu 解题思路 分层图最短路,层与层之间的边的边权减半,然后就是板子了. 细节注意事项 咕咕咕. 参考代码 #include <algorithm> #include < ...

  6. 「POI2015」KIN

    传送门 Luogu 解题思路 想要做这道题,只要会维护区间最大子段和就好了. 而这个可以用线段树维护模板点这里 对于重复的情况,我们可以对每一个位置记一个前驱表示和当前位置种类相同的前一个位置. 然后 ...

  7. IDEA代码检验出错

    该软件功能过于强大,会自动检验执行所需要的代码,所以会报错,但实际上我们有写 例如 解决方法 File-->setting 将error改为warning

  8. 1-5SpringBoot操作之Spring-Data-Jpa(二)CRUD实现

    本帖来演示下SpringBoot下,实用Spring-Data-Jpa来实现CRUD操作,视图层采用Freemarker 这里我们先把application.properties修改成applicat ...

  9. git+jenkins jar包代码的发布加新建项目

    1.本地仓库  java开发 把代码上传上来 ,问一下他要上传到的主机ip , 分支 2.本地 , 设置-->仓库 更新数据,让他同步到南阳gitlab, 若没有这个项目,需要创建相同名字的项目 ...

  10. JAVA(windows)安装教程

    JAVA(windows)安装教程 一.下载: https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133 ...