Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
yangxiujun
/
paidan_demo
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit dde19761
authored
Jun 15, 2023
by
丁伟峰
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feat-dingwf-mvp616' into develop
2 parents
3104b30e
5f9e01e9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
4 deletions
project-order/src/main/java/com/alibaba/cloud/integration/order/controller/GlobalExceptionHandler.java
project-order/src/main/java/com/alibaba/cloud/integration/order/controller/GlobalExceptionHandler.java
View file @
dde1976
...
...
@@ -20,30 +20,34 @@ import java.util.stream.Collectors;
@RestControllerAdvice
public
class
GlobalExceptionHandler
{
private
static
final
String
BAD_REQUEST_MSG
=
"参数检验不通过"
;
//处理 form data方式调用接口校验失败抛出的异常
@ExceptionHandler
(
BindException
.
class
)
public
Result
<?>
bindExceptionHandler
(
BindException
e
)
{
List
<
FieldError
>
fieldErrors
=
e
.
getBindingResult
().
getFieldErrors
();
List
<
String
>
collect
=
fieldErrors
.
stream
().
map
(
DefaultMessageSourceResolvable:
:
getDefaultMessage
).
collect
(
Collectors
.
toList
());
return
Result
.
instance
(
400
,
BAD_REQUEST_MSG
,
collect
);
return
Result
.
failed
(
String
.
join
(
";"
,
collect
)
);
}
// 处理 json 请求体调用接口校验失败抛出的异常
@ExceptionHandler
(
MethodArgumentNotValidException
.
class
)
public
Result
<?>
methodArgumentNotValidExceptionHandler
(
MethodArgumentNotValidException
e
)
{
List
<
FieldError
>
fieldErrors
=
e
.
getBindingResult
().
getFieldErrors
();
List
<
String
>
collect
=
fieldErrors
.
stream
().
map
(
DefaultMessageSourceResolvable:
:
getDefaultMessage
).
collect
(
Collectors
.
toList
());
return
Result
.
instance
(
400
,
BAD_REQUEST_MSG
,
collect
);
return
Result
.
failed
(
String
.
join
(
";"
,
collect
)
);
}
// 处理单个参数校验失败抛出的异常
@ExceptionHandler
(
ConstraintViolationException
.
class
)
public
Result
<?>
constraintViolationExceptionHandler
(
ConstraintViolationException
e
)
{
Set
<
ConstraintViolation
<?>>
constraintViolations
=
e
.
getConstraintViolations
();
List
<
String
>
collect
=
constraintViolations
.
stream
().
map
(
ConstraintViolation:
:
getMessage
).
collect
(
Collectors
.
toList
());
return
Result
.
instance
(
400
,
BAD_REQUEST_MSG
,
collect
);
return
Result
.
failed
(
String
.
join
(
";"
,
collect
)
);
}
// 处理以上处理不了的其他异常
@ExceptionHandler
(
Exception
.
class
)
public
Result
<?>
exceptionHandler
(
Exception
e
)
{
return
Result
.
instance
(
400
,
BAD_REQUEST_MSG
,
e
.
getMessage
(
));
return
Result
.
failed
(
String
.
join
(
";"
,
collect
));
}
}
Write
Preview
Markdown
is supported
Attach a file
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 post a comment