4 删除页面
用户操作流程:
1、用户进入用户列表,点击“删除”
2、执行删除操作,提示“删除成功”或“删除失败”
4.1 删除页面接口定义
1
2
|
@ApiOperation
(
"通过ID删除页面"
)
public ResponseResult
delete
(
String
id
)
;
|
4.2 删除页面服务端开发
4.2.1Dao
使用 Spring Data提供的deleteById方法完成删除操作 。
4.2.2 Service
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
/
/
删除页面
public ResponseResult
delete
(
String
id
)
{
CmsPage one
=
this.
getById
(
id
)
;
if
(
one!
=
null
)
{
/
/
删除页面
cmsPageRepository
.deleteById
(
id
)
;
return
new
ResponseResult
(
CommonCode.SUCCESS
)
;
}
return
new
ResponseResult
(
CommonCode.FAIL
)
;
}
|
4.2.3Controller
01
02
03
04
05
06
07
08
09
10
11
12
|
@DeleteMapping
(
"
/del/{id}
"
)
/
/
使用http的
delete
方法完成岗位操作
public ResponseResult
delete
(
@PathVariable
(
"
id
"
)
String
id
)
{
return
pageService.
delete
(
id
)
;
}
|
4.3 删除页面前端开发
4.3.1 Api方法
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
/
*
页面删除
*
/
export const page_del
=
id
=
>
{
return
http
.requestDelete
(
apiUrl
+
'
/
cms
/
page
/
del
/
'
+
id
)
}
|
4.3.2编写页面
1、在page_list.vue页面添加删除按钮
01
02
03
04
05
06
07
08
09
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
|
<
el
‐
table
‐
column
label
=
"
操作
"
width
=
"
120
"
>
<
template slot
‐
scope
=
"
page
"
>
<
el
‐
button
size
=
"
small
"
type
=
"
text
"
@click
=
"
edit(page.row.
pageId)
"
>
编辑
<
/
el
‐
button
>
<
el
‐
button
size
=
"
small
"
type
=
"
text
"
@click
=
"
del(page.row.
pageId)
"
>
删除
<
/
el
‐
button
>
<
/
template
>
<
/
el
‐
table
‐
column
>
|
2、删除事件
01
02
03
04
05
06
07
08
09
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
|
/
/
删除
del
:
function
(
pageId
)
{
this.
$confirm
(
'
确认删除此页面吗?
'
,
'
提示
'
,
{
}
)
.
then
(
(
)
=
>
{
cmsApi.
page_del
(
pageId
)
.
then
(
(
res
)
=
>
{
if
(
res.success
)
{
this.
$
message
(
{
type
:
'
success
'
,
message
:
'
删除成功!
'
}
)
;
/
/
查询页面
this.
query
(
)
}
else
{
this.
$
message
(
{
type
:
'
error
'
,
message
:
'
删除失败!
'
}
)
;
}
}
)
}
)
}
|
5 异常处理
5.1 异常处理的问题分析
从添加页面的service方法中找问题:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
|
/
/
添加页面
public CmsPageResult
add
(
CmsPage cmsPage
)
{
/
/
校验页面是否存在,根据页面名称、站点Id、页面webpath查询
CmsPage cmsPage
1
=
cmsPageRepository
.findByPageNameAndSiteIdAndPageWebPath
(
cmsPage.
getPageName
(
)
,
cmsPage.
getSiteId
(
)
,
cmsPage.
getPageWebPath
(
)
)
;
if
(
cmsPage
1
=
=
null
)
{
cmsPage.setPageId
(
null
)
;
/
/
添加页面主键由spring
data
自动生成
cmsPageRepository
.
save
(
cmsPage
)
;
/
/
返回结果
CmsPageResult cmsPageResult
=
new
CmsPageResult
(
CommonCode.SUCCESS
,
cmsPage
)
;
return
cmsPageResult;
}
return
new
CmsPageResult
(
CommonCode.FAIL
,
null
)
;
}
|
问题:
1 、上边的代码只要操作不成功仅向用户返回 “ 错误代码: 11111 ,失败信息:操作失败 ” ,无法区别具体的错误信 息。
2 、 service 方法在执行过程出现异常在哪捕获?在 service 中需要都加 try/catch ,如果在 controller 也需要添加 try/catch ,代码冗余严重且不易维护。
解决方案:
1 、在 Service 方法中的编码顺序是先校验判断,有问题则抛出具体的异常信息,最后执行具体的业务操作,返回成 功信息。
2 、在统 一 异常处理类中去捕获异常,无需 controller 捕获异常,向用户返回统 一 规范的响应信息。
代码模板如下:
01
02
03
04
05
06
07
08
09
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
|
/
/
添加页面
public CmsPageResult
add
(
CmsPage cmsPage
)
{
/
/
校验cmsPage是否为空
if
(
cmsPage
=
=
null
)
{
/
/
抛出异常,非法请求
/
/
...
}
/
/
根据页面名称查询(页面名称已在mongodb创建了唯
一
索引)
CmsPage cmsPage
1
=
cmsPageRepository
.findByPageNameAndSiteIdAndPageWebPath
(
cmsPage.
getPageName
(
)
,
cmsPage.
getSiteId
(
)
,
cmsPage.
getPageWebPath
(
)
)
;
/
/
校验页面是否存在,已存在则抛出异常
if
(
cmsPage
1
!
=
null
)
{
/
/
抛出异常,已存在相同的页面名称
/
/
...
}
cmsPage.setPageId
(
null
)
;
/
/
添加页面主键由spring
data
自动生成
CmsPage
save
=
cmsPageRepository
.
save
(
cmsPage
)
;
/
/
返回结果
CmsPageResult cmsPageResult
=
new
CmsPageResult
(
CommonCode.SUCCESS
,
save
)
;
return
cmsPageResult;
}
|
5.2 异常处理流程
系统对异常的处理使用统一的异常处理流程:
1、自定义异常类型。
2、自定义错误代码及错误信息。
3、对于可预知的异常由程序员在代码中主动抛出,由SpringMVC统一捕获。
可预知异常是程序员在代码中手动抛出本系统定义的特定异常类型,由于是程序员抛出的异常,通常异常信息比较齐全,程序员在抛出时会指定错误代码及错误信息,获取异常信息也比较方便。
4、对于不可预知的异常(运行时异常)由SpringMVC统一捕获Exception类型的异常。不可预知异常通常是由于系统出现bug、或一些不要抗拒的错误(比如网络中断、服务器宕机等),异常类型为RuntimeException类型(运行时异常)。
5、可预知的异常及不可预知的运行时异常最终会采用统一的信息格式(错误代码+错误信息)来表示,最终也会随请求响应给客户端。
异常抛出及处理流程
1、在controller、service、dao中程序员抛出自定义异常;springMVC框架抛出框架异常类型
2、统一由异常捕获类捕获异常,并进行处理
3、捕获到自定义异常则直接取出错误代码及错误信息,响应给用户。
4、捕获到非自定义异常类型首先从Map中找该异常类型是否对应具体的错误代码,如果有则取出错误代码和错误信息并响应给用户,如果从Map中找不到异常类型所对应的错误代码则统一为99999错误代码并响应给用户。
5、将错误代码及错误信息以Json格式响应给用户。
5.3 可预知异常处理
5.3.1 自定义异常类
在common工程定义异常类型。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.xuecheng
.framework.exception;
import com.xuecheng
.framework.model.response.ResultCode;
public
class
CustomException extends RuntimeException
{
private ResultCode resultCode;
public CustomException
(
ResultCode resultCode
)
{
/
/
异常信息为错误代码
+
异常信息
super
(
"
错误代码:
"
+
resultCode.code
(
)
+
"
错误信息:
"
+
resultCode.
message
(
)
)
;
this.resultCode
=
resultCode;
}
public ResultCode getResultCode
(
)
{
return
this.resultCode;
}
}
|
01
02
03
04
05
06
07
08
09
10
|
package com.xuecheng
.framework.exception;
import com.xuecheng
.framework.model.response.ResultCode;
public
class
ExceptionCast
{
/
/
使用此静态方法抛出自定义异常
public static void cast
(
ResultCode resultCode
)
{
throw
new
CustomException
(
resultCode
)
;
}
}
|
5.3.3 异常捕获类
使用 @ControllerAdvice 和 @ExceptionHandler 注解来捕获指定类型的异常
01
02
03
04
05
06
07
08
09
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
|
package com.xuecheng
.framework.exception;
import com.xuecheng
.framework.model.response.ResponseResult;
import com.xuecheng
.framework.model.response.ResultCode;
import org
.slf
4
j
.Logger;
import org
.slf
4
j
.LoggerFactory;
import org
.springframework.web.bind.annotation.ControllerAdvice;
import org
.springframework.web.bind.annotation.ExceptionHandler;
import org
.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public
class
ExceptionCatch
{
private static final Logger LOGGER
=
LoggerFactory
.
getLogger
(
ExceptionCatch.
class
)
;
/
/
捕获 CustomException异常
@ExceptionHandler
(
CustomException.
class
)
@ResponseBody
public ResponseResult customException
(
CustomException e
)
{
LOGGER.
error
(
"
catch exception : {}\r\nexception:
"
,
e.
getMessage
(
)
,
e
)
;
ResultCode resultCode
=
e.
getResultCode
(
)
;
ResponseResult responseResult
=
new
ResponseResult
(
resultCode
)
;
return
responseResult;
}
}
|
5.3.4 异常处理测试
5.3.4.1 定义错误代码
每个业务操作的异常使用异常代码去标识。
01
02
03
04
05
06
07
08
09
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
|
package com.xuecheng
.framework.domain.cms.response;
import com.xuecheng
.framework.model.response.ResultCode;
import lombok.ToString;
@ToString
public enum CmsCode implements ResultCode
{
CMS_ADDPAGE_EXISTS
(
false
,
24001
,
"
页面已存在!
"
)
;
/
/
操作结果
boolean
success;
/
/
操作代码
int code;
/
/
提示信息
String
message
;
private CmsCode
(
boolean
success
,
int code
,
String
message
)
{
this.success
=
success;
this.code
=
code;
this.
message
=
message
;
}
@Override
public
boolean
success
(
)
{
return
success;
}
@Override
public int code
(
)
{
return
code;
}
@Override
public String
message
(
)
{
return
message
;
}
}
|
5.3.4.2 异常处理测试
1 、抛出异常
在 controller 、 service 、 dao 中都可以抛出异常。
修改 PageService 的 add 方法,添加抛出异常的代码
01
02
03
04
05
06
07
08
09
10
11
12
13
|
/
校验页面是否存在,根据页面名称、站点Id、页面webpath查询
CmsPage cmsPage
1
=
cmsPageRepository
.findByPageNameAndSiteIdAndPageWebPath
(
cmsPage.
getPageName
(
)
,
cmsPage.
getSiteId
(
)
,
cmsPage.
getPageWebPath
(
)
)
;
if
(
cmsPage
1
!
=
null
)
{
/
/
校验页面是否存在,已存在则抛出异常
ExceptionCast.cast
(
CmsCode.CMS_ADDPAGE_EXISTS
)
;
}
|
2 、启动工程,扫描到异常捕获的类 ExceptionCatch
1
2
3
4
5
6
7
8
|
在springBoot的启动类中添加
@ComponentScan
(
basePackages
=
"
com.xuecheng
.framework
"
)
/
/
扫描common工程下的类
|
3 、前端展示异常信息
服务端响应信息如下:

页面提取异常处理
01
02
03
04
05
06
07
08
09
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
|
addSubmit
(
)
{
this.
$refs.
pageForm.validate
(
(
valid
)
=
>
{
if
(
valid
)
{
this.
$confirm
(
'
确认提交吗?
'
,
'
提示
'
,
{
}
)
.
then
(
(
)
=
>
{
cmsApi.
page_add
(
this.
pageForm
)
.
then
(
(
res
)
=
>
{
console.
log
(
res
)
;
if
(
res.success
)
{
this.
$
message
(
{
message
:
'
提交成功
'
,
type
:
'
success
'
}
)
;
this.
$refs[
'
pageForm
'
]
.resetFields
(
)
;
}
else if
(
res.
message
)
{
this.
$
message
.
error
(
res.
message
)
;
}
else
{
this.
$
message
.
error
(
'
提交失败
'
)
;
}
}
)
;
}
)
;
}
}
)
;
}
|
5.4 不可预知异常处理
5.4.1 定义异常捕获方法
5.4.1.1 异常抛出测试
使用postman测试添加页面,不输入cmsPost信息,提交,报错信息如下:
1
2
3
4
|
org
.springframework.http
.converter.HttpMessageNotReadableException
此异常是springMVC在进行参数转换时报的错误。
|
01
02
03
04
05
06
07
08
09
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
|
{
"
timestamp
"
:
1528712906727
,
"
status
"
:
400
,
"
error
"
:
"
Bad Request
"
,
"
exception
"
:
"
org
.springframework.http
.converter.HttpMessageNotReadableException
"
,
"
message
"
:
"
Required request body is missing: public
com.xuecheng
.framework.domain.cms.response.CmsPageResult
com.xuecheng
.manage_cms.web.controller.CmsPageController.add(com.xuecheng
.framework.domain.cms.C
msPage)
"
,
"
path
"
:
"
/cms/page/add
"
}
|
上边的响应信息在客户端是无法解析的。
在异常捕获类中添加对 Exception 异常的捕获:
01
02
03
04
05
06
07
08
09
10
11
12
|
@ExceptionHandler
(
Exception.
class
)
@ResponseBody
public ResponseResult exception
(
Exception exception
)
{
/
/
记录日志
LOGGER.
error
(
"
catch exception:{}
"
,
exception.
getMessage
(
)
)
;
return
null;
}
|
5.4.1.2 异常捕获方法
针对上边的问题其解决方案是:
1 、我们在 map 中配置 HttpMessageNotReadableException 和错误代码。
2 、在异常捕获类中对 Exception 异常进行捕获,并从 map 中获取异常类型对应的错误代码,如果存在错误代码则返 回此错误,否则统 一 返回 99999 错误。
具体的开发实现如下:
1 、在通用错误代码类 CommCode 中配置非法参数异常
1
2
3
4
5
|
INVALID_PARAM
(
false
,
10003
,
"
非法参数!
"
)
,
|
异常捕获类代码如下:
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
|
package com.xuecheng
.framework.exception;
import com.
google.common.collect.ImmutableMap
import com.xuecheng
.framework.model.response.CommonCode;
import com.xuecheng
.framework.model.response.ResponseResult;
import com.xuecheng
.framework.model.response.ResultCode;
import org
.slf
4
j
.Logger;
import org
.slf
4
j
.LoggerFactory;
import org
.springframework.http
.converter.HttpMessageNotReadableException;
import org
.springframework.web.bind.annotation.ControllerAdvice;
import org
.springframework.web.bind.annotation.ExceptionHandler;
import org
.springframework.web.bind.annotation.ResponseBody;
/
*
*
*
@author Administrator
*
@
version
1.0
*
@create
2018
‐
06
‐
11
17
:
16
*
*
/
@ControllerAdvice
public
class
ExceptionCatch
{
private static final Logger LOGGER
=
LoggerFactory
.
getLogger
(
ExceptionCatch.
class
)
;
/
/
使用EXCEPTIONS存放异常类型和错误代码的映射,ImmutableMap的特点的
一
旦创建不可改变,并且线程安全
private static ImmutableMap
<
Class
<
? extends Throwable
>
,
ResultCode
>
EXCEPTIONS;
/
/
使用builder来构建
一
个异常类型和错误代码的异常
protected static ImmutableMap
.Builder
<
Class
<
? extends Throwable
>
,
ResultCode
>
builder
=
ImmutableMap
.builder
(
)
;
/
/
捕获Exception异常
@ResponseBody
@ExceptionHandler
(
Exception.
class
)
public ResponseResult exception
(
Exception e
)
{
LOGGER.
error
(
"
catch exception : {}\r\nexception:
"
,
e.
getMessage
(
)
,
e
)
;
if
(
EXCEPTIONS
=
=
null
)
EXCEPTIONS
=
builder.build
(
)
;
final ResultCode resultCode
=
EXCEPTIONS.
get
(
e.
getClass
(
)
)
;
final ResponseResult responseResult;
if
(
resultCode !
=
null
)
{
responseResult
=
new
ResponseResult
(
resultCode
)
;
}
else
{
responseResult
=
new
ResponseResult
(
CommonCode.SERVER_ERROR
)
;
}
return
responseResult;
}
/
/
捕获 CustomException异常
@ExceptionHandler
(
CustomException.
class
)
@ResponseBody
public ResponseResult customException
(
CustomException e
)
{
LOGGER.
error
(
"
catch exception : {}\r\nexception:
"
,
e.
getMessage
(
)
,
e
)
;
ResultCode resultCode
=
e.
getResultCode
(
)
;
ResponseResult responseResult
=
new
ResponseResult
(
resultCode
)
;
return
responseResult;
}
static
{
/
/
在这里加入
一
些基础的异常类型判断
builder.
put
(
HttpMessageNotReadableException.
class
,
CommonCode.INVALIDPARAM
)
;
}
}
|
5.4.3 异常处理测试
仍然模拟
“
问题测试
”
中的测试步骤,异常结果为
“
非法参数
”
。
6 实战
此部分为自学内容,根据今天所学知识完成下边的任务。
6.1 查询条件完善
页面查询条件增加:页面名称、页面类型。
页面名称对应CmsPage模型类中的pageName属性。
页面类型对应CmsPage模型类中的pageType属性。
查询要求:
页面名称:模糊查询
页面类型:精确匹配,页面类型包括:静态和动态,在数据库中静态用“0”表示,动态用“1”表示。
6.2 页面属性增加DataUrl
在CmsPage.
java模型类型中有一个dataUrl属性,此属性在页面静态化时需要填写。
本需求要求:
1、在新增页面增加dataUrl输入框,并支持添加。
2、在修改页面增加dataUrl输入框,并支持修改。
推荐学习:
视频链接:http://yun.itheima.com/course/295.html?stt
提取码:epxm
20天企业开发实战品优购电商系统开发下(11-20天+配置资料源码)
视频链接:https://pan.baidu.com/s/1RJK9jumxt9MARAWohy1E3Q
提取码:5b74
资料链接:https://pan.baidu.com/s/1kXYpm76eJwqXnEpWE2pXMQ
提取码:otpa