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 9ec5955d
authored
Jun 15, 2023
by
丁伟峰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
for test
1 parent
02ac9b99
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
36 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 @
9ec5955
...
...
@@ -2,54 +2,47 @@ package com.alibaba.cloud.integration.order.controller;
import
com.alibaba.cloud.integration.common.Result
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.
Object
Error
;
import
org.springframework.validation.
Field
Error
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
javax.validation.ConstraintViolation
;
import
javax.validation.ConstraintViolationException
;
import
javax.validation.ValidationException
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.stream.Collectors
;
@Slf4j
@RestControllerAdvice
public
class
GlobalExceptionHandler
{
@ExceptionHandler
(
value
=
{
BindException
.
class
,
ValidationException
.
class
,
MethodArgumentNotValidException
.
class
,
Exception
.
class
})
public
Result
<?>
handleValidatedException
(
Exception
e
)
{
Result
<?>
resp
=
null
;
log
.
error
(
"=======$$$$$$$$$$$$======"
+
e
.
toString
());
if
(
e
instanceof
MethodArgumentNotValidException
)
{
// BeanValidation exception
MethodArgumentNotValidException
ex
=
(
MethodArgumentNotValidException
)
e
;
resp
=
Result
.
failed
(
ex
.
getBindingResult
().
getAllErrors
().
stream
()
.
map
(
ObjectError:
:
getDefaultMessage
)
.
collect
(
Collectors
.
joining
(
"; "
))
);
}
else
if
(
e
instanceof
ConstraintViolationException
)
{
// BeanValidation GET simple param
ConstraintViolationException
ex
=
(
ConstraintViolationException
)
e
;
resp
=
Result
.
failed
(
ex
.
getConstraintViolations
().
stream
()
.
map
(
ConstraintViolation:
:
getMessage
)
.
collect
(
Collectors
.
joining
(
"; "
))
);
}
else
if
(
e
instanceof
BindException
)
{
// BeanValidation GET object param
BindException
ex
=
(
BindException
)
e
;
resp
=
Result
.
failed
(
ex
.
getAllErrors
().
stream
()
.
map
(
ObjectError:
:
getDefaultMessage
)
.
collect
(
Collectors
.
joining
(
"; "
))
);
}
else
{
resp
=
Result
.
failed
(
"其他错误信息,这个逻辑应该不会走到 ~~~ "
);
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
(
o
->
o
.
getDefaultMessage
()).
collect
(
Collectors
.
toList
());
return
Result
.
instance
(
HttpStatus
.
BAD_REQUEST
.
value
(),
BAD_REQUEST_MSG
,
collect
);
}
return
resp
;
// 处理 json 请求体调用接口校验失败抛出的异常
@ExceptionHandler
(
MethodArgumentNotValidException
.
class
)
public
Result
<?>
methodArgumentNotValidExceptionHandler
(
MethodArgumentNotValidException
e
)
{
List
<
FieldError
>
fieldErrors
=
e
.
getBindingResult
().
getFieldErrors
();
List
<
String
>
collect
=
fieldErrors
.
stream
().
map
(
o
->
o
.
getDefaultMessage
()).
collect
(
Collectors
.
toList
());
return
Result
.
instance
(
HttpStatus
.
BAD_REQUEST
.
value
(),
BAD_REQUEST_MSG
,
collect
);
}
// 处理单个参数校验失败抛出的异常
@ExceptionHandler
(
ConstraintViolationException
.
class
)
public
Result
<?>
constraintViolationExceptionHandler
(
ConstraintViolationException
e
)
{
Set
<
ConstraintViolation
<?>>
constraintViolations
=
e
.
getConstraintViolations
();
List
<
String
>
collect
=
constraintViolations
.
stream
().
map
(
o
->
o
.
getMessage
()).
collect
(
Collectors
.
toList
());
return
Result
.
instance
(
HttpStatus
.
BAD_REQUEST
.
value
(),
BAD_REQUEST_MSG
,
collect
);
}
// 处理以上处理不了的其他异常
@ExceptionHandler
(
Exception
.
class
)
public
Result
<?>
exceptionHandler
(
Exception
e
)
{
return
Result
.
instance
(
HttpStatus
.
BAD_REQUEST
.
value
(),
BAD_REQUEST_MSG
,
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