共三个步骤:
一、pom.xm新增:
<!--
使用
JRS-303
校验数据
-->
<dependency>
<groupId>
org.hibernate
</groupId>
<artifactId>
hibernate-validator
</artifactId>
<version>
5.1.3.Final
</version>
</dependency>
<dependency>
<groupId>
javax.validation
</groupId>
<artifactId>
validation-api
</artifactId>
<version>
1.1.0.Final
</version>
</dependency>
<dependency>
<groupId>
org.jboss.logging
</groupId>
<artifactId>
jboss-logging
</artifactId>
<version>
3.1.1.GA
</version>
</dependency>
二、配置切面编程Aspect
2.1新建切面处理类,见附件1:ValidAspect 类
2.2.springmvc-servlet.xml中添加如下:
<!--切面开启-->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
三、在controller层加入注释:如下
3.1参数加标记符如下:
/**
* Created by linzetian on 2016/11/10.
* Description:
登录信息
*/
public class
Login {
@NotNull
(
message
=
"
登录名不能为空
"
)
private
String
name
;
//
登录名
@NotNull
(
message
=
"
密码不能为空
"
)
private
String
password
;
//
密码
public
String
getName
() {
return
name
;
}
public void
setName
(String name) {
this
.
name
= name
;
}
public
String
getPassword
() {
return
password
;
}
public void
setPassword
(String password) {
this
.
password
= password
;
}
}
3.2 在controller层加标记符如下:
@ResponseBody
public
Result
login
(
@RequestBody @Valid
Login bean
,
BindingResult result){
return new
Result().success(
"
操作成功
"
)
;
}
注意:@
Valid 注释表示要校验的参数,其后必须跟
BindingResult
变量
附件:
===============附件1
:ValidAspect 类
package
com.tecsun.das.collect.common.aspect
;
import
com.tecsun.das.collect.common.result.RESULT_CODES
;
import
com.tecsun.das.collect.common.result.Result
;
import
com.tecsun.das.collect.common.util.StringUtils
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.reflect.MethodSignature
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.validation.ObjectError
;
import
javax.validation.Valid
;
import
javax.xml.validation.Validator
;
import
java.lang.annotation.Annotation
;
import
java.lang.reflect.Method
;
/**
* Created by linzetian on 2016/11/10.
* Description:controller
层校验切面类
*
主要用户校验数据
*/
@Component
@Aspect
public class
ValidAspect {
/**
*
含
ResponseBody
标识的方法进入切面编程
*
@param
pjp
*
@return
*
@throws
Throwable
*/
@Around
(
"@annotation(org.springframework.web.bind.annotation.ResponseBody)"
)
public
Object
doValid
(ProceedingJoinPoint pjp)
throws
Throwable{
MethodSignature signature = (MethodSignature) pjp.getSignature()
;
Method method = signature.getMethod()
;
if
(!Result.
class
.equals(method.getReturnType())){
pjp.proceed()
;
}
Object[] args = pjp.getArgs()
;
Annotation[][] annotations = method.getParameterAnnotations()
;
for
(
int
i =
0
;
i < annotations.
length
;
i++){
if
(!hasValidAnnotation(annotations[i])){
continue;
}
if
(!(i < annotations.
length
-
1
&& args[i+
1
]
instanceof
BindingResult)){
//
验证对象后面没有跟
bindingResult,
事实上如果没有应该到不了这一步
continue;
}
BindingResult result = (BindingResult) args[i+
1
]
;
Result rt =
getError
(result)
;
if
(RESULT_CODES.
FAILL
.equals(rt.getStatusCode())){
return
rt
;
}
}
return
pjp.proceed()
;
}
/**
*
判断是否包含
Valid
和
Validated
标签
*
@param
annotations
*
@return
*/
private boolean
hasValidAnnotation
(Annotation[] annotations){
if
(annotations ==
null
){
return false;
}
for
(Annotation annotation : annotations){
if
(annotation
instanceof
Valid
|| annotation
instanceof
Validated
){
return true;
}
}
return false;
}
/**
*
获取校验失败的错误信息
*
@param
result
校验的结果
*
@return
校验通过
“”
校验失败:错误信息
*/
private static
Result
getError
(BindingResult result){
try
{
StringBuilder errorMg =
new
StringBuilder()
;
if
(result.hasErrors()){
for
(ObjectError error : result.getAllErrors()){
String fieldName = ((FieldError) error).getField()
;
errorMg.append(fieldName+error.getDefaultMessage()+
","
)
;
}
String errors = StringUtils.
trimEnd
(errorMg.toString()
,
","
)
;
return new
Result().fail(errors)
;
}
}
catch
(Exception e) {
e.printStackTrace()
;
}
return new
Result().success(
"
校验通过
"
)
;
}
}