Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gic-webapp-plug
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
base_platform_enterprise
gic-webapp-plug
Commits
17752986
Commit
17752986
authored
May 27, 2021
by
xuwenqian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:增加统一参数校验
parent
1b1d2b0a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
1 deletions
+87
-1
GoodsDomainController.java
.../gic/plug/web/controller/goods/GoodsDomainController.java
+1
-1
SpringGlobalExceptionHandler.java
.../gic/plug/web/exception/SpringGlobalExceptionHandler.java
+86
-0
No files found.
src/main/java/com/gic/plug/web/controller/goods/GoodsDomainController.java
View file @
17752986
...
...
@@ -143,7 +143,7 @@ public class GoodsDomainController extends BaseGoodsController {
GoodsChannelVO
goodsChannelVO
=
new
GoodsChannelVO
();
goodsChannelVO
.
setChannelCode
(
entry
.
getKey
());
goodsChannelVO
.
setChannelName
(
Constant
.
CHANNEL_CODE_AND_NAME
.
get
(
entry
.
getKey
()));
goodsChannelVO
.
setSceneCode
(
Constant
.
CHANNEL_CODE_AND_SCENE
.
get
(
entry
.
getKey
()));
goodsChannelVO
.
setSceneCode
(
Constant
.
ES_CODES_MAP
.
get
(
entry
.
getKey
()));
goodsChannelVO
.
setGoodsDomainList
(
new
ArrayList
<>());
for
(
GoodsDomainDTO
goodsDomainDTO
:
entry
.
getValue
())
{
GoodsDomainVO
goodsDomainVO
=
new
GoodsDomainVO
();
...
...
src/main/java/com/gic/plug/web/exception/SpringGlobalExceptionHandler.java
0 → 100644
View file @
17752986
package
com
.
gic
.
plug
.
web
.
exception
;
import
com.gic.commons.exception.BaseRuntimeException
;
import
com.gic.commons.webapi.reponse.RestResponse
;
import
com.gic.enterprise.exception.CommonException
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.FieldError
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.validation.ConstraintViolation
;
import
javax.validation.ConstraintViolationException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Set
;
/**
* 统一参数校验
*
* @author xuwenqian
*/
@RestControllerAdvice
public
class
SpringGlobalExceptionHandler
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
SpringGlobalExceptionHandler
.
class
);
@ExceptionHandler
(
value
=
Exception
.
class
)
public
RestResponse
handlerException
(
Exception
exception
)
{
String
message
=
exception
.
getMessage
();
return
RestResponse
.
failure
(
com
.
gic
.
goods
.
api
.
dto
.
ErrorCode
.
UNKNOW_ERROR
.
getCode
(),
com
.
gic
.
goods
.
api
.
dto
.
ErrorCode
.
UNKNOW_ERROR
.
getErrorMessage
());
}
@ExceptionHandler
(
value
=
NullPointerException
.
class
)
public
RestResponse
nullExceptionHandler
(
HttpServletRequest
req
,
NullPointerException
e
)
{
log
.
error
(
"发生空指针异常!原因是:"
,
e
);
return
RestResponse
.
failure
(
com
.
gic
.
goods
.
api
.
dto
.
ErrorCode
.
UNKNOW_ERROR
.
getCode
(),
e
.
getMessage
());
}
/**
* 统一参数校验
*
* @param ex
* @return
*/
@ExceptionHandler
(
BindException
.
class
)
public
RestResponse
handleBindException
(
BindException
ex
)
{
FieldError
fieldError
=
ex
.
getBindingResult
().
getFieldError
();
return
RestResponse
.
failure
(
com
.
gic
.
enterprise
.
error
.
ErrorCode
.
MISS_PARAMETER
.
getCode
(),
fieldError
.
getDefaultMessage
());
}
@ExceptionHandler
(
value
=
MethodArgumentNotValidException
.
class
)
public
RestResponse
handleNotValid
(
MethodArgumentNotValidException
exception
)
{
String
message
=
exception
.
getBindingResult
().
getFieldError
().
getDefaultMessage
();
return
RestResponse
.
failure
(
com
.
gic
.
goods
.
api
.
dto
.
ErrorCode
.
INVALIDATE_PARAM
.
getCode
(),
message
);
}
@ExceptionHandler
(
value
=
ConstraintViolationException
.
class
)
public
RestResponse
methodConstraintViolationException
(
ConstraintViolationException
exception
)
{
List
<
String
>
msg
=
new
ArrayList
<>();
Set
<
ConstraintViolation
<?>>
constraintViolations
=
exception
.
getConstraintViolations
();
RestResponse
result
=
new
RestResponse
();
result
.
setCode
(
com
.
gic
.
goods
.
api
.
dto
.
ErrorCode
.
INVALIDATE_PARAM
.
getCode
());
for
(
ConstraintViolation
<?>
constraintViolation
:
constraintViolations
)
{
msg
.
add
(
constraintViolation
.
getMessage
());
}
result
.
setMessage
(
StringUtils
.
join
(
msg
,
","
));
return
result
;
}
@ExceptionHandler
(
value
=
CommonException
.
class
)
public
RestResponse
enterpriseCommonException
(
CommonException
exception
)
{
return
RestResponse
.
failure
(
exception
.
getErrorCode
(),
exception
.
getMessage
());
}
@ExceptionHandler
(
value
=
BaseRuntimeException
.
class
)
public
RestResponse
baseRuntimeException
(
BaseRuntimeException
exception
)
throws
Exception
{
return
RestResponse
.
failure
(
exception
.
getErrCode
(),
exception
.
getMessage
());
}
}
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