- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章SpringBoot中定制异常页面的实现方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
定制异常页面,可以避免用户产生恐慌心理,使得产品有更好的用户体验。今天来学习在 SpringBoot 中如何定制开发异常页面 。
1、历史回顾 。
在 SpringMVC 年代,我们的异常页面一般配置在 web.xml 文件中,如下:
1
2
3
4
5
|
<!-- 配置404页面 -->
<
error-page
>
<
error-code
>404</
error-code
>
<
location
>/error/404.html</
location
>
</
error-page
>
|
这里我们指定了异常请求状态码 404,然后配置了 404 异常请求的页面地址,这就意味着如果某一个请求发生了 404 异常,则会出现 404.html 界面 。
2、SpringBoot中配置 。
1、默认异常页面 。
这是 SpringBoot 中默认的异常页面,返回的是一堆异常信息和异常状态码,那用户固然是看不懂这些信息的,容易使得用户产生恐慌的心里,从而影响产品的用户体验 。
2、定制异常页面 。
SpringBoot 中定制异常页面非常简单,我们需要一个配置文件 ExceptionPageConfig.java 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
import
org.springframework.boot.web.server.ConfigurableWebServerFactory;
import
org.springframework.boot.web.server.ErrorPage;
import
org.springframework.boot.web.server.WebServerFactoryCustomizer;
import
org.springframework.context.annotation.Bean;
import
org.springframework.context.annotation.Configuration;
import
org.springframework.http.HttpStatus;
/**
* 统一异常页面处理
*
* @Author Lizhou
**/
@Configuration
public
class
ExceptionPageConfig {
/**
* SpringBoot2.0以上版本
* WebServerFactoryCustomizer代替之前版本的EmbeddedWebServerFactoryCustomizerAutoConfiguration
*
* @return
*/
@Bean
public
WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
return
(container -> {
ErrorPage error400Page =
new
ErrorPage(HttpStatus.BAD_REQUEST,
"/400"
);
ErrorPage error404Page =
new
ErrorPage(HttpStatus.NOT_FOUND,
"/404"
);
ErrorPage error500Page =
new
ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,
"/500"
);
container.addErrorPages(error400Page, error404Page, error500Page);
});
}
}
|
可以看出,这里我们配置了 400、404、500 三种异常页面,然后我们需要编写 API 请求异常页面 SysExceptionController.java 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
import
com.zyxx.common.utils.PasswordUtils;
import
io.swagger.annotations.Api;
import
io.swagger.annotations.ApiOperation;
import
org.apache.shiro.SecurityUtils;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.GetMapping;
/**
* <p>
* 前端控制器
* </p>
*
* @author lizhou
* @since 2020-07-15
**/
@Api
(tags =
"后台管理端--异常处理"
)
@Controller
public
class
SysExceptionController {
@ApiOperation
(value =
"请求400页面"
, notes =
"请求400页面"
)
@GetMapping
(
"400"
)
public
String badRequest() {
return
"sys/exception/400"
;
}
@ApiOperation
(value =
"请求404页面"
, notes =
"请求404页面"
)
@GetMapping
(
"404"
)
public
String notFound() {
return
"sys/exception/404"
;
}
@ApiOperation
(value =
"请求500页面"
, notes =
"请求500页面"
)
@GetMapping
(
"500"
)
public
String serverError() {
return
"sys/exception/500"
;
}
}
|
API 写好,下面我们就需要开发异常页面的展示信息了,这里贴一张页面吧,404.html 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
import com.zyxx.common.utils.PasswordUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
/**
* <
p
>
* 前端控制器
* </
p
>
*
* @author lizhou
* @since 2020-07-15
**/
@Api(tags = "后台管理端--异常处理")
@Controller
public class SysExceptionController {
@ApiOperation(value = "请求400页面", notes = "请求400页面")
@GetMapping("400")
public String badRequest() {
return "sys/exception/400";
}
@ApiOperation(value = "请求404页面", notes = "请求404页面")
@GetMapping("404")
public String notFound() {
return "sys/exception/404";
}
@ApiOperation(value = "请求500页面", notes = "请求500页面")
@GetMapping("500")
public String serverError() {
return "sys/exception/500";
}
}
|
然后所需要的 exception.css 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
.error .clip .shadow {
height
:
180px
;
}
.error .
clip
:nth-of-type(
2
) .shadow {
width
:
130px
;
}
.error .
clip
:nth-of-type(
1
) .shadow, .error .
clip
:nth-of-type(
3
) .shadow {
width
:
250px
;
}
.error .digit {
width
:
150px
;
height
:
150px
;
line-height
:
150px
;
font-size
:
120px
;
font-weight
:
bold
;
}
.error h
2
{
font-size
:
32px
;
}
.error .msg {
top
:
-190px
;
left
:
30%
;
width
:
80px
;
height
:
80px
;
line-height
:
80px
;
font-size
:
32px
;
}
.error span.triangle {
top
:
70%
;
right
:
0%
;
border-left
:
20px
solid
#535353
;
border-top
:
15px
solid
transparent
;
border-bottom
:
15px
solid
transparent
;
}
.error .container-error
-404
{
top
:
50%
;
margin-top
:
250px
;
position
:
relative
;
height
:
250px
;
padding-top
:
40px
;
}
.error .container-error
-404
.clip {
display
: inline-
block
;
transform: skew(
-45
deg);
}
.error .clip .shadow {
overflow
:
hidden
;
}
.error .
clip
:nth-of-type(
2
) .shadow {
overflow
:
hidden
;
position
:
relative
;
box-shadow:
inset
20px
0px
20px
-15px
rgba(
150
,
150
,
150
,
0.8
),
20px
0px
20px
-15px
rgba(
150
,
150
,
150
,
0.8
);
}
.error .
clip
:nth-of-type(
3
) .shadow:after, .error .
clip
:nth-of-type(
1
) .shadow:after {
content
:
""
;
position
:
absolute
;
right
:
-8px
;
bottom
:
0px
;
z-index
:
9999
;
height
:
100%
;
width
:
10px
;
background
: linear-gradient(
90
deg,
transparent
, rgba(
173
,
173
,
173
,
0.8
),
transparent
);
border-radius:
50%
;
}
.error .
clip
:nth-of-type(
3
) .shadow:after {
left
:
-8px
;
}
.error .digit {
position
:
relative
;
top
:
8%
;
color
:
white
;
background
:
#1E9FFF
;
border-radius:
50%
;
display
: inline-
block
;
transform: skew(
45
deg);
}
.error .
clip
:nth-of-type(
2
) .digit {
left
:
-10%
;
}
.error .
clip
:nth-of-type(
1
) .digit {
right
:
-20%
;
}
.error .
clip
:nth-of-type(
3
) .digit {
left
:
-20%
;
}
.error h
2
{
font-size
:
24px
;
color
:
#A2A2A2
;
font-weight
:
bold
;
padding-bottom
:
20px
;
}
.error .tohome {
font-size
:
16px
;
color
:
#07B3F9
;
}
.error .msg {
position
:
relative
;
z-index
:
9999
;
display
:
block
;
background
:
#535353
;
color
:
#A2A2A2
;
border-radius:
50%
;
font-style
:
italic
;
}
.error .triangle {
position
:
absolute
;
z-index
:
999
;
transform: rotate(
45
deg);
content
:
""
;
width
:
0
;
height
:
0
;
}
@media (
max-width
:
767px
) {
.error .clip .shadow {
height
:
100px
;
}
.error .
clip
:nth-of-type(
2
) .shadow {
width
:
80px
;
}
.error .
clip
:nth-of-type(
1
) .shadow, .error .
clip
:nth-of-type(
3
) .shadow {
width
:
100px
;
}
.error .digit {
width
:
80px
;
height
:
80px
;
line-height
:
80px
;
font-size
:
52px
;
}
.error h
2
{
font-size
:
18px
;
}
.error .msg {
top
:
-110px
;
left
:
15%
;
width
:
40px
;
height
:
40px
;
line-height
:
40px
;
font-size
:
18px
;
}
.error span.triangle {
top
:
70%
;
right
:
-3%
;
border-left
:
10px
solid
#535353
;
border-top
:
8px
solid
transparent
;
border-bottom
:
8px
solid
transparent
;
}
.error .container-error
-404
{
height
:
150px
;
}
}
|
所需要的 exception.js 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
.error .clip .shadow {
height: 180px;
}
.error .clip:nth-of-type(2) .shadow {
width: 130px;
}
.error .clip:nth-of-type(1) .shadow, .error .clip:nth-of-type(3) .shadow {
width: 250px;
}
.error .digit {
width: 150px;
height: 150px;
line-height: 150px;
font-size: 120px;
font-weight: bold;
}
.error h2 {
font-size: 32px;
}
.error .msg {
top: -190px;
left: 30%;
width: 80px;
height: 80px;
line-height: 80px;
font-size: 32px;
}
.error span.triangle {
top: 70%;
right: 0%;
border-left: 20px solid
#535353;
border-top: 15px solid transparent;
border-bottom: 15px solid transparent;
}
.error .container-error-404 {
top: 50%;
margin-top: 250px;
position: relative;
height: 250px;
padding-top: 40px;
}
.error .container-error-404 .clip {
display: inline-block;
transform: skew(-45deg);
}
.error .clip .shadow {
overflow: hidden;
}
.error .clip:nth-of-type(2) .shadow {
overflow: hidden;
position: relative;
box-shadow: inset 20px 0px 20px -15px rgba(150, 150, 150, 0.8), 20px 0px 20px -15px rgba(150, 150, 150, 0.8);
}
.error .clip:nth-of-type(3) .shadow:after, .error .clip:nth-of-type(1) .shadow:after {
content:
""
;
position: absolute;
right: -8px;
bottom: 0px;
z-index: 9999;
height: 100%;
width: 10px;
background: linear-gradient(90deg, transparent, rgba(173, 173, 173, 0.8), transparent);
border-radius: 50%;
}
.error .clip:nth-of-type(3) .shadow:after {
left: -8px;
}
.error .digit {
position: relative;
top: 8%;
color: white;
background:
#1E9FFF;
border-radius: 50%;
display: inline-block;
transform: skew(45deg);
}
.error .clip:nth-of-type(2) .digit {
left: -10%;
}
.error .clip:nth-of-type(1) .digit {
right: -20%;
}
.error .clip:nth-of-type(3) .digit {
left: -20%;
}
.error h2 {
font-size: 24px;
color:
#A2A2A2;
font-weight: bold;
padding-bottom: 20px;
}
.error .tohome {
font-size: 16px;
color:
#07B3F9;
}
.error .msg {
position: relative;
z-index: 9999;
display: block;
background:
#535353;
color:
#A2A2A2;
border-radius: 50%;
font-style: italic;
}
.error .triangle {
position: absolute;
z-index: 999;
transform: rotate(45deg);
content:
""
;
width: 0;
height: 0;
}
@media (max-width: 767px) {
.error .clip .shadow {
height: 100px;
}
.error .clip:nth-of-type(2) .shadow {
width: 80px;
}
.error .clip:nth-of-type(1) .shadow, .error .clip:nth-of-type(3) .shadow {
width: 100px;
}
.error .digit {
width: 80px;
height: 80px;
line-height: 80px;
font-size: 52px;
}
.error h2 {
font-size: 18px;
}
.error .msg {
top: -110px;
left: 15%;
width: 40px;
height: 40px;
line-height: 40px;
font-size: 18px;
}
.error span.triangle {
top: 70%;
right: -3%;
border-left: 10px solid
#535353;
border-top: 8px solid transparent;
border-bottom: 8px solid transparent;
}
.error .container-error-404 {
height: 150px;
}
}
|
3、测试 。
项目启动后,我们访问一个并不存在的 API 。
那,通过访问一个项目中并不存在的 API,得到 404 页面,页面可以提示一些友好的文字,从而安抚用户紧张的心理,其实也是一个不错的选择吧 。
到此这篇关于SpringBoot中定制异常页面的实现方法的文章就介绍到这了,更多相关SpringBoot 定制异常页面内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/qq_40065776/article/details/107863418 。
最后此篇关于SpringBoot中定制异常页面的实现方法的文章就讲到这里了,如果你想了解更多关于SpringBoot中定制异常页面的实现方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
问题很简单:我正在寻找一种优雅的使用方式 CompletableFuture#exceptionally与 CompletableFuture#supplyAsync 一起.这是行不通的: priva
对于 Web 服务,我们通常使用 maven-jaxb2-plugin 生成 java bean,并在 Spring 中使用 JAXB2 编码。我想知道如何处理 WSDL/XSD 中声明的(SOAP-
这个问题已经有答案了: Array index out of bound behavior (10 个回答) 已关闭 8 年前。 我对下面的 C 代码感到好奇 int main(){
当在类的开头使用上下文和资源初始化 MediaPlayer 对象时,它会抛出 NullPointer 异常,但是当在类的开头声明它时(因此它是 null),然后以相同的方式初始化它在onCreate方
嘿 我尝试将 java 程序连接到 REST API。 使用相同的代码部分,我在 Java 6 中遇到了 Java 异常,并且在 Java 8 中运行良好。 环境相同: 信任 机器 unix 用户 代
我正在尝试使用 Flume 和 Hive 进行 Twitter 分析。为了从 twitter 获取推文,我在 flume.conf 文件中设置了所有必需的参数(consumerKey、consumer
我在 JavaFX 异常方面遇到一些问题。我的项目在我的 Eclipse 中运行,但现在我的 friend 也尝试访问该项目。我们已共享并直接保存到保管箱文件夹中。但他根本无法让它发挥作用。他在控制台
假设我使用 blur() 事件验证了电子邮件 ID,我正在这样做: $('#email').blur(function(){ //make ajax call , check if dupli
我这样做是为了从 C 代码调用非托管函数。 pCallback 是一个函数指针,因此在托管端是一个委托(delegate)。 [DllImport("MyDLL.dll")] public stati
为什么这段代码是正确的: try { } catch(ArrayOutOfBoundsException e) {} 这是错误的: try { } catch(IOException e) {} 这段
我遇到了以下问题:有导出函数的DLL。 代码示例如下:[动态链接库] __declspec(dllexport) int openDevice(int,void**) [应用] 开发者.h: __de
从其他线程,我知道我们不应该在析构函数中抛出异常!但是对于下面的例子,它确实有效。这是否意味着我们只能在一个实例的析构函数中抛出异常?我们应该如何理解这个代码示例! #include using n
为什么需要异常 引出 public static void main(String[
1. Java的异常机制 Throwable类是Java异常类型的顶层父类,一个对象只有是 Throwable 类的(直接或者间接)实例,他才是一个异常对象,才能被异常处理机制识别。JDK中内
我是 Python 的新手,我对某种异常方法的实现有疑问。这是代码(缩写): class OurException(Exception): """User defined Exception"
我已经创建了以下模式来表示用户和一组线程之间的关联,这些线程按他们的最后一条消息排序(用户已经阅读了哪些线程,哪些没有): CREATE TABLE table(user_id bigint, mes
我正在使用 Python 编写一个简单的自动化脚本,它可能会在多个位置引发异常。在他们每个人中,我都想记录一条特定的消息并退出程序。为此,我在捕获异常并处理它(执行特定的日志记录操作等)后引发 Sys
谁能解释一下为什么这会导致错误: let xs = [| "Mary"; "Mungo"; "Midge" |] Array.iter printfn xs 虽然不是这样: Array.iter pr
在我使用 Play! 的网站上,我有一个管理部分。所有 Admin Controller 都有一个 @With 和一个 @Check 注释。 断开连接后,一切正常。连接后,每次加载页面(任何页面,无论
我尝试连接到 azure 表存储并添加一个对象。它在本地主机上工作得很好,但是在我使用的服务器上我得到以下异常及其内部异常: Exception of type 'Microsoft.Wind
我是一名优秀的程序员,十分优秀!