1、总览

springboot - 映射HTTP Response Status Codes 到 FreeMarker Error页面-LMLPHP

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、执行

springboot - 映射HTTP Response Status Codes 到 FreeMarker Error页面-LMLPHP

springboot - 映射HTTP Response Status Codes 到 FreeMarker Error页面-LMLPHP

springboot - 映射HTTP Response Status Codes 到 FreeMarker Error页面-LMLPHP

springboot - 映射HTTP Response Status Codes 到 FreeMarker Error页面-LMLPHP

总结:

1、目录结构为:

templates

|_error.ftlh

|_error

  |_5xx.ftlh

  |_404.ftlh

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

server.error.whitelabel.enabled=false

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

server.error.include-stacktrace=always
05-08 15:44