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 738f0ceb
authored
Jun 16, 2023
by
丁伟峰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
for test
1 parent
4fcb2061
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
43 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 @
738f0ce
package
com
.
alibaba
.
cloud
.
integration
.
order
.
controller
;
package
com
.
alibaba
.
cloud
.
integration
.
order
.
controller
;
import
com.alibaba.cloud.integration.common.Result
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.FieldError
;
import
org.springframework.validation.FieldError
;
import
org.springframework.validation.ObjectError
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
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.Res
tControllerAdvice
;
import
org.springframework.web.bind.annotation.Res
ponseBody
;
import
java.util.ArrayList
;
import
java.util.Objects
;
import
java.util.List
;
@ControllerAdvice
@Slf4j
@Slf4j
@RestControllerAdvice
public
class
GlobalExceptionHandler
{
public
class
GlobalExceptionHandler
{
@ExceptionHandler
(
MethodArgumentNotValidException
.
class
)
@ExceptionHandler
(
Exception
.
class
)
public
ResponseEntity
<
ErrorResponse
>
handleValidationException
(
MethodArgumentNotValidException
ex
)
{
@ResponseBody
List
<
String
>
errors
=
new
ArrayList
<>();
public
Result
<?>
handleException
(
Exception
e
)
{
for
(
FieldError
error
:
ex
.
getBindingResult
().
getFieldErrors
())
{
if
(
e
instanceof
MethodArgumentNotValidException
)
{
errors
.
add
(
error
.
getDefaultMessage
());
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
());
}
}
ErrorResponse
errorResponse
=
new
ErrorResponse
(
HttpStatus
.
BAD_REQUEST
,
"Validation failed"
,
errors
);
return
ResponseEntity
.
badRequest
().
body
(
errorResponse
);
}
@ExceptionHandler
(
BindException
.
class
)
public
ResponseEntity
<
ErrorResponse
>
handleBindException
(
BindException
ex
)
{
log
.
error
(
"$$$$$$$$$$$$$$$$$$$$ ====> {}"
,
ex
.
toString
());
List
<
String
>
errors
=
new
ArrayList
<>();
for
(
ObjectError
error
:
ex
.
getBindingResult
().
getAllErrors
())
{
errors
.
add
(
error
.
getDefaultMessage
());
}
ErrorResponse
errorResponse
=
new
ErrorResponse
(
HttpStatus
.
BAD_REQUEST
,
"Binding failed"
,
errors
);
return
ResponseEntity
.
badRequest
().
body
(
errorResponse
);
}
// 其他异常处理方法...
// 定义错误响应类
private
static
class
ErrorResponse
{
private
final
int
status
;
private
final
String
message
;
private
final
List
<
String
>
errors
;
public
ErrorResponse
(
HttpStatus
status
,
String
message
,
List
<
String
>
errors
)
{
this
.
status
=
status
.
value
();
this
.
message
=
message
;
this
.
errors
=
errors
;
}
// 省略 getter 方法...
}
}
}
}
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