Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
ldp-docs
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
doc
ldp-docs
Commits
5aaa6cf7
Commit
5aaa6cf7
authored
Sep 23, 2020
by
马超
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增业务异常使用方式
parent
df16100a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
0 deletions
+47
-0
biz-exception.png
imgs/biz-exception.png
+0
-0
Example样例工程开发文档.md
开发文档/Example样例工程开发文档.md
+47
-0
No files found.
imgs/biz-exception.png
0 → 100644
View file @
5aaa6cf7
37.2 KB
开发文档/Example样例工程开发文档.md
View file @
5aaa6cf7
...
...
@@ -882,4 +882,51 @@ isOut字段标识接口类型,false:外部系统调用本系统,true:本
```
## 七、业务异常处理
LDP业务异常类是
`BizException`
,最简单的使用方式就是,传入code和message,并抛出BizException
```
java
try
{
......
......
}
catch
{
throw
new
BizException
(
500
,
"业务异常"
)
}
//或者
if
(
condition
)
{
throw
new
BizException
(
500
,
"业务异常"
)
}
```
有时候我们需要考虑国际化,需要在startup模块下的resources目录中国际化文件中写上对应的code和message,当浏览器为英文时,使用en_US文件中的code和message,否则使用zh_CN文件,如果两个文件都没写,则默认使用
**messages.properties**
中的code以及message。

写好code和message后,注入MessageSourceHandler,并调用newBizException方法,传入去掉code和message的错误编码。以下是样例,如果删除的id值为“error-1”则抛出业务异常(参考脚手架工程ExampleRest类)。
```
java
@Autowired
MessageSourceHandler
messageSourceHandler
;
/**
* 删除用户
*
* @param id
* @return
*/
@PostMapping
(
"/del/{id}"
)
public
RestResult
deleteUser
(
@PathVariable
(
"id"
)
String
id
)
{
if
(
"error-1"
.
equals
(
id
))
{
throw
messageSourceHandler
.
newBizException
(
"example.biz.demo.del.error"
);
}
ExampleUserInfo
userInfo
=
new
ExampleUserInfo
(
id
);
exampleService
.
delete
(
userInfo
);
return
new
RestResult
(
ResultMsg
.
SUCCESS
,
""
);
}
```
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment