Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gic-enterprise-base
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-enterprise-base
Commits
cde62f55
Commit
cde62f55
authored
Aug 15, 2019
by
guojuxing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
service曾参数校验
parent
3fe11e3e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
116 additions
and
0 deletions
+116
-0
pom.xml
gic-enterprise-base-api/pom.xml
+6
-0
ValidParamsUtils.java
...java/com/gic/enterprise/utils/valid/ValidParamsUtils.java
+110
-0
No files found.
gic-enterprise-base-api/pom.xml
View file @
cde62f55
...
...
@@ -49,6 +49,12 @@
<version>
4.12
</version>
<scope>
test
</scope>
</dependency>
<!--参数验证-->
<dependency>
<groupId>
org.hibernate
</groupId>
<artifactId>
hibernate-validator
</artifactId>
<version>
6.0.16.Final
</version>
</dependency>
<dependency>
<groupId>
com.gic
</groupId>
<artifactId>
gic-log-api
</artifactId>
...
...
gic-enterprise-base-api/src/main/java/com/gic/enterprise/utils/valid/ValidParamsUtils.java
0 → 100644
View file @
cde62f55
package
com
.
gic
.
enterprise
.
utils
.
valid
;
import
java.util.Iterator
;
import
java.util.Set
;
import
javax.validation.ConstraintViolation
;
import
javax.validation.Validation
;
import
javax.validation.Validator
;
import
com.gic.enterprise.exception.CommonException
;
import
org.hibernate.validator.HibernateValidator
;
import
com.gic.api.base.commons.ServiceResponse
;
import
com.gic.enterprise.error.ErrorCode
;
/**
* service层面的参数校验工具
* @ClassName: ValidUtil
* @Description:
* @author guojuxing
* @date 2019/7/9 10:49 AM
*/
public
class
ValidParamsUtils
{
/**
* 开启快速结束模式 failFast (true)
*/
private
static
Validator
failFastValidator
=
Validation
.
byProvider
(
HibernateValidator
.
class
).
configure
()
.
failFast
(
true
).
buildValidatorFactory
().
getValidator
();
/**
* 全部校验
*/
private
static
Validator
validator
=
Validation
.
buildDefaultValidatorFactory
().
getValidator
();
private
ValidParamsUtils
()
{
}
/**
* 输出第一个参数错误的信息
* @Title: fastFailValidate
* @Description:
* @author guojuxing
* @param obj
* @param groups 分组接口
* @return com.gic.api.base.commons.ServiceResponse
*/
public
static
<
T
>
ServiceResponse
fastFailValidate
(
T
obj
,
Class
<?>...
groups
)
{
Set
<
ConstraintViolation
<
T
>>
constraintViolations
=
failFastValidator
.
validate
(
obj
,
groups
);
if
(
constraintViolations
.
size
()
>
0
)
{
String
paramName
=
constraintViolations
.
iterator
().
next
().
getPropertyPath
().
toString
();
String
paramError
=
constraintViolations
.
iterator
().
next
().
getMessage
();
return
ServiceResponse
.
failure
(
ErrorCode
.
PARAMETER_ERROR
.
getCode
(),
getFailFastMsg
(
paramName
,
paramError
));
}
return
ServiceResponse
.
success
();
}
/**
* 输出所有参数错误的信息
* @Title: allCheckValidate
* @Description:
* @author guojuxing
* @param obj
* @param groups
* @return com.gic.api.base.commons.ServiceResponse
*/
public
static
<
T
>
ServiceResponse
allCheckValidate
(
T
obj
,
Class
<?>...
groups
)
{
Set
<
ConstraintViolation
<
T
>>
constraintViolations
=
validator
.
validate
(
obj
,
groups
);
if
(
constraintViolations
.
size
()
>
0
)
{
StringBuilder
errorMessages
=
new
StringBuilder
();
Iterator
<
ConstraintViolation
<
T
>>
iterator
=
constraintViolations
.
iterator
();
while
(
iterator
.
hasNext
())
{
ConstraintViolation
<
T
>
violation
=
iterator
.
next
();
errorMessages
.
append
(
getFailFastMsg
(
violation
.
getPropertyPath
().
toString
(),
violation
.
getMessage
()))
.
append
(
","
);
}
return
ServiceResponse
.
failure
(
ErrorCode
.
PARAMETER_ERROR
.
getCode
(),
errorMessages
.
toString
());
}
return
ServiceResponse
.
success
();
}
/**
* 输出所有参数错误的信息
* @Title: allCheckValidate
* @Description:
* @author guojuxing
* @param obj
* @param groups
* @return com.gic.api.base.commons.ServiceResponse
*/
public
static
<
T
>
void
allCheckValidateOfException
(
T
obj
,
Class
<?>...
groups
)
{
Set
<
ConstraintViolation
<
T
>>
constraintViolations
=
validator
.
validate
(
obj
,
groups
);
if
(
constraintViolations
.
size
()
>
0
)
{
StringBuilder
errorMessages
=
new
StringBuilder
();
Iterator
<
ConstraintViolation
<
T
>>
iterator
=
constraintViolations
.
iterator
();
while
(
iterator
.
hasNext
())
{
ConstraintViolation
<
T
>
violation
=
iterator
.
next
();
errorMessages
.
append
(
getFailFastMsg
(
violation
.
getPropertyPath
().
toString
(),
violation
.
getMessage
()))
.
append
(
","
);
}
throw
new
CommonException
(
ErrorCode
.
PARAMETER_ERROR
.
getCode
(),
errorMessages
.
toString
());
}
}
public
static
String
getFailFastMsg
(
String
paramName
,
String
paramError
)
{
return
String
.
format
(
"%s:%s"
,
paramName
,
paramError
);
}
}
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