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 5f9e01e9
authored
Jun 15, 2023
by
丁伟峰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
for test
1 parent
a5b007ca
Show 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 @
5f9e01e
...
@@ -20,30 +20,34 @@ import java.util.stream.Collectors;
...
@@ -20,30 +20,34 @@ import java.util.stream.Collectors;
@RestControllerAdvice
@RestControllerAdvice
public
class
GlobalExceptionHandler
{
public
class
GlobalExceptionHandler
{
private
static
final
String
BAD_REQUEST_MSG
=
"参数检验不通过"
;
private
static
final
String
BAD_REQUEST_MSG
=
"参数检验不通过"
;
//处理 form data方式调用接口校验失败抛出的异常
//处理 form data方式调用接口校验失败抛出的异常
@ExceptionHandler
(
BindException
.
class
)
@ExceptionHandler
(
BindException
.
class
)
public
Result
<?>
bindExceptionHandler
(
BindException
e
)
{
public
Result
<?>
bindExceptionHandler
(
BindException
e
)
{
List
<
FieldError
>
fieldErrors
=
e
.
getBindingResult
().
getFieldErrors
();
List
<
FieldError
>
fieldErrors
=
e
.
getBindingResult
().
getFieldErrors
();
List
<
String
>
collect
=
fieldErrors
.
stream
().
map
(
DefaultMessageSourceResolvable:
:
getDefaultMessage
).
collect
(
Collectors
.
toList
());
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 请求体调用接口校验失败抛出的异常
// 处理 json 请求体调用接口校验失败抛出的异常
@ExceptionHandler
(
MethodArgumentNotValidException
.
class
)
@ExceptionHandler
(
MethodArgumentNotValidException
.
class
)
public
Result
<?>
methodArgumentNotValidExceptionHandler
(
MethodArgumentNotValidException
e
)
{
public
Result
<?>
methodArgumentNotValidExceptionHandler
(
MethodArgumentNotValidException
e
)
{
List
<
FieldError
>
fieldErrors
=
e
.
getBindingResult
().
getFieldErrors
();
List
<
FieldError
>
fieldErrors
=
e
.
getBindingResult
().
getFieldErrors
();
List
<
String
>
collect
=
fieldErrors
.
stream
().
map
(
DefaultMessageSourceResolvable:
:
getDefaultMessage
).
collect
(
Collectors
.
toList
());
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
)
@ExceptionHandler
(
ConstraintViolationException
.
class
)
public
Result
<?>
constraintViolationExceptionHandler
(
ConstraintViolationException
e
)
{
public
Result
<?>
constraintViolationExceptionHandler
(
ConstraintViolationException
e
)
{
Set
<
ConstraintViolation
<?>>
constraintViolations
=
e
.
getConstraintViolations
();
Set
<
ConstraintViolation
<?>>
constraintViolations
=
e
.
getConstraintViolations
();
List
<
String
>
collect
=
constraintViolations
.
stream
().
map
(
ConstraintViolation:
:
getMessage
).
collect
(
Collectors
.
toList
());
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
)
@ExceptionHandler
(
Exception
.
class
)
public
Result
<?>
exceptionHandler
(
Exception
e
)
{
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