Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cloud-backend
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
framework
cloud-backend
Commits
76a46596
Commit
76a46596
authored
Jun 20, 2024
by
袁伟铭
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化代码
parent
9c02c1a3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
2 deletions
+9
-2
xxx-common-utils/src/main/java/com/zq/common/config/base/UnifiedExceptionHandler.java
+9
-2
No files found.
xxx-common-utils/src/main/java/com/zq/common/config/base/UnifiedExceptionHandler.java
View file @
76a46596
...
@@ -12,6 +12,8 @@ import org.springframework.dao.DataAccessException;
...
@@ -12,6 +12,8 @@ import org.springframework.dao.DataAccessException;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.converter.HttpMessageConversionException
;
import
org.springframework.http.converter.HttpMessageConversionException
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.FieldError
;
import
org.springframework.web.HttpMediaTypeNotSupportedException
;
import
org.springframework.web.HttpMediaTypeNotSupportedException
;
import
org.springframework.web.HttpRequestMethodNotSupportedException
;
import
org.springframework.web.HttpRequestMethodNotSupportedException
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
...
@@ -22,6 +24,8 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
...
@@ -22,6 +24,8 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import
org.springframework.web.multipart.support.MissingServletRequestPartException
;
import
org.springframework.web.multipart.support.MissingServletRequestPartException
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
java.util.List
;
import
java.util.stream.Collectors
;
/**
/**
* API接口统一异常处理类
* API接口统一异常处理类
...
@@ -81,7 +85,7 @@ public class UnifiedExceptionHandler {
...
@@ -81,7 +85,7 @@ public class UnifiedExceptionHandler {
return
ResultVo
.
fail
(
HttpStatus
.
BAD_REQUEST
.
value
(),
"无法解析请求消息"
);
return
ResultVo
.
fail
(
HttpStatus
.
BAD_REQUEST
.
value
(),
"无法解析请求消息"
);
}
}
@ExceptionHandler
({
MissingServletRequestPartException
.
class
,
MissingServletRequestParameterException
.
class
})
@ExceptionHandler
({
MissingServletRequestPartException
.
class
,
MissingServletRequestParameterException
.
class
,
BindException
.
class
})
public
ResultVo
handleMissingServletRequestPartException
(
Exception
ex
,
HttpServletRequest
request
)
{
public
ResultVo
handleMissingServletRequestPartException
(
Exception
ex
,
HttpServletRequest
request
)
{
log
.
warn
(
">> missing servlet request part/param error: {} {}"
,
request
.
getRequestURI
(),
ex
.
getMessage
());
log
.
warn
(
">> missing servlet request part/param error: {} {}"
,
request
.
getRequestURI
(),
ex
.
getMessage
());
String
paranmName
=
""
;
String
paranmName
=
""
;
...
@@ -89,8 +93,11 @@ public class UnifiedExceptionHandler {
...
@@ -89,8 +93,11 @@ public class UnifiedExceptionHandler {
paranmName
=
((
MissingServletRequestPartException
)
ex
).
getRequestPartName
();
paranmName
=
((
MissingServletRequestPartException
)
ex
).
getRequestPartName
();
}
else
if
(
ex
instanceof
MissingServletRequestParameterException
)
{
}
else
if
(
ex
instanceof
MissingServletRequestParameterException
)
{
paranmName
=
((
MissingServletRequestParameterException
)
ex
).
getParameterName
();
paranmName
=
((
MissingServletRequestParameterException
)
ex
).
getParameterName
();
}
else
if
(
ex
instanceof
BindException
)
{
List
<
FieldError
>
fieldErrors
=
((
BindException
)
ex
).
getBindingResult
().
getFieldErrors
();
paranmName
=
fieldErrors
.
stream
().
map
(
f
->
"["
+
f
.
getDefaultMessage
()
+
"("
+
f
.
getField
()
+
")]"
).
collect
(
Collectors
.
joining
(
"、"
));
}
}
return
ResultVo
.
fail
(
HttpStatus
.
BAD_REQUEST
.
value
(),
"缺少请求参数"
+
paranmName
);
return
ResultVo
.
fail
(
HttpStatus
.
BAD_REQUEST
.
value
(),
"缺少请求参数
:
"
+
paranmName
);
}
}
@ExceptionHandler
(
DataAccessException
.
class
)
@ExceptionHandler
(
DataAccessException
.
class
)
...
...
Write
Preview
Markdown
is supported
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