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
;
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
com.alibaba.cloud.integration.common.Result
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.FieldError
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
java.util.Objects
;
@ControllerAdvice
@RestControllerAdvice
@Slf4j
public
class
GlobalExceptionHandler
{
public
class
GlobalExceptionHandler
{
// 处理 BindException 异常
@ExceptionHandler
(
BindException
.
class
)
@ExceptionHandler
(
BindException
.
class
)
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
// 可选:设置响应状态码
@ResponseBody
@ResponseBody
public
Result
<?>
handleBindException
(
BindException
ex
)
{
public
Result
<?>
handleBindException
(
BindException
e
)
{
// 处理异常并返回自定义的错误信息
// 处理 BindException 异常并返回自定义错误信息
log
.
error
(
"======== xxxxxxxxxxx ====> {}"
,
ex
.
toString
());
return
Result
.
failed
(
"Invalid request parameters"
);
return
Result
.
failed
(
"请求参数验证失败:"
+
ex
.
getMessage
());
}
}
@ExceptionHandler
(
Exception
.
class
)
@ExceptionHandler
(
Exception
.
class
)
@ResponseBody
@ResponseBody
public
Result
<?>
handleException
(
Exception
e
)
{
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
());
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