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 704fedf2
authored
Jun 16, 2023
by
丁伟峰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
for test
1 parent
2062cc96
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
7 additions
and
24 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 @
704fedf
package
com
.
alibaba
.
cloud
.
integration
.
order
.
controller
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.validation.BindException
;
import
com.alibaba.cloud.integration.common.Result
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.validation.FieldError
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.validation.BindException
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
import
java.util.Objects
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
@ControllerAdvice
@Slf4j
@RestControllerAdvice
public
class
GlobalExceptionHandler
{
// 处理 BindException 异常
@ExceptionHandler
(
BindException
.
class
)
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
// 可选:设置响应状态码
@ResponseBody
public
Result
<?>
handleBindException
(
BindException
ex
)
{
// 处理异常并返回自定义的错误信息
log
.
error
(
"======== xxxxxxxxxxx ====> {}"
,
ex
.
toString
());
return
Result
.
failed
(
"请求参数验证失败:"
+
ex
.
getMessage
());
public
Result
<?>
handleBindException
(
BindException
e
)
{
// 处理 BindException 异常并返回自定义错误信息
return
Result
.
failed
(
"Invalid request parameters"
);
}
@ExceptionHandler
(
Exception
.
class
)
@ResponseBody
public
Result
<?>
handleException
(
Exception
e
)
{
if
(
e
instanceof
MethodArgumentNotValidException
)
{
log
.
error
(
"======== MethodArgumentNotValidException ====> {}"
,
e
.
toString
());
FieldError
e1
=
((
MethodArgumentNotValidException
)
e
).
getBindingResult
().
getFieldError
();
return
Result
.
failed
(
Objects
.
requireNonNull
(
e1
).
getDefaultMessage
());
}
else
{
log
.
error
(
"====== 其他错误 ===={}"
,
e
.
toString
());
return
Result
.
failed
(
e
.
getMessage
());
}
}
}
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