自定义数据校验

2018-03-01 11:20:14来源:oschina作者:haoyuehong人点击

分享
package com.hz1202.utils;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hz1202.exception.ParamException;
import org.apache.commons.collections.MapUtils;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.*;
/**
* @Author: mol
* @Description:数据校验工具
* @Date: create in 9:12 2018/2/27
*/
public class BeanValidator {
//创建校验工厂
private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
public static Map validate(T t,Class... groups){
Validator validator = validatorFactory.getValidator();
//获取校验结果
Set validateResult = validator.validate(t,groups);
//判断结果是否为空
if(validateResult.isEmpty()){
//返回空集合
return Collections.emptyMap();
}else{
LinkedHashMap errors = Maps.newLinkedHashMap();
Iterator iterator = validateResult.iterator();
while(iterator.hasNext()){
ConstraintViolation violation = (ConstraintViolation)iterator.next();
errors.put(violation.getPropertyPath().toString(),violation.getMessage());
}
return errors;
}
}
public static Map validateList(Collection<?> collection){
Preconditions.checkNotNull(collection);
Iterator iterator = collection.iterator();
Map errors;
do{
if(!iterator.hasNext()){
return Collections.emptyMap();
}
Object object = iterator.next();
errors = validate(object,new Class[0]);
}while (errors.isEmpty());
return errors;
}
public static Map validateObject(Object first,Object... objects){
if(objects != null && objects.length>0){
return validateList(Lists.asList(first,objects));
}else{
return validate(first,new Class[0]);
}
}
public static void check(Object param)throws ParamException{
Map map = validateObject(param);
if(MapUtils.isNotEmpty(map)){
throw new ParamException(map.toString());
}
}
}

测试参数:


package com.hz1202.param;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.validator.constraints.NotBlank;
import javax.validation.constraints.NotNull;
/**
* @Author: mol
* @Description:
* @Date: create in 10:26 2018/2/27
*/
@Setter
@Getter
public class TestVO {
@NotBlank
private String msg;
@NotNull
private Integer id;
}

测试方法:


package com.hz1202.controller;
import com.hz1202.common.JsonData;
import com.hz1202.exception.ParamException;
import com.hz1202.exception.PermissionException;
import com.hz1202.param.TestVO;
import com.hz1202.utils.BeanValidator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;@Controller
@RequestMapping(value = "/test")
@Slf4j
public class TestController {
@RequestMapping("/validate.json")
@ResponseBody
public JsonData testValidate(TestVO testVO) throws ParamException{
log.info("【校验测试】校验测试");
BeanValidator.check(testVO);
return JsonData.createSuccess("test validate");
}
}

浏览器访问不带参数时 http://localhost:8080/test/validate.json输出



说明校验成功,继续访问http://localhost:8080/test/validate.json?id=1&msg=“haha”输出



校验生效

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台