Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
7a7fb53e
Unverified
Commit
7a7fb53e
authored
Oct 24, 2022
by
jiandan217
Committed by
GitHub
Oct 24, 2022
Browse files
perf: 优化接口数据验证异常 message (#771)
parent
e0af08eb
Changes
1
Show whitespace changes
Inline
Side-by-side
eladmin-common/src/main/java/me/zhengjie/exception/handler/GlobalExceptionHandler.java
View file @
7a7fb53e
...
@@ -23,10 +23,11 @@ import me.zhengjie.utils.ThrowableUtil;
...
@@ -23,10 +23,11 @@ import me.zhengjie.utils.ThrowableUtil;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.security.authentication.BadCredentialsException
;
import
org.springframework.security.authentication.BadCredentialsException
;
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.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
java.util.Objects
;
import
static
org
.
springframework
.
http
.
HttpStatus
.*;
import
static
org
.
springframework
.
http
.
HttpStatus
.*;
/**
/**
...
@@ -95,11 +96,10 @@ public class GlobalExceptionHandler {
...
@@ -95,11 +96,10 @@ public class GlobalExceptionHandler {
public
ResponseEntity
<
ApiError
>
handleMethodArgumentNotValidException
(
MethodArgumentNotValidException
e
){
public
ResponseEntity
<
ApiError
>
handleMethodArgumentNotValidException
(
MethodArgumentNotValidException
e
){
// 打印堆栈信息
// 打印堆栈信息
log
.
error
(
ThrowableUtil
.
getStackTrace
(
e
));
log
.
error
(
ThrowableUtil
.
getStackTrace
(
e
));
String
[]
str
=
Objects
.
requireNonNull
(
e
.
getBindingResult
().
getAllErrors
().
get
(
0
).
getCodes
())[
1
].
split
(
"\\."
);
ObjectError
objectError
=
e
.
getBindingResult
().
getAllErrors
().
get
(
0
);
String
message
=
e
.
getBindingResult
().
getAllErrors
().
get
(
0
).
getDefaultMessage
();
String
message
=
objectError
.
getDefaultMessage
();
String
msg
=
"不能为空"
;
if
(
objectError
instanceof
FieldError
)
{
if
(
msg
.
equals
(
message
)){
message
=
((
FieldError
)
objectError
).
getField
()
+
": "
+
message
;
message
=
str
[
1
]
+
":"
+
message
;
}
}
return
buildResponseEntity
(
ApiError
.
error
(
message
));
return
buildResponseEntity
(
ApiError
.
error
(
message
));
}
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
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 comment