背景:
-
我们的接口是一个List对象,对象里面的数据基本都有一些基础数据校验的注解,我们怎么样才能校验这些基础规则呢?
-
我们在导入excel文件进行数据录入的时候,数据录入也有基础的校验规则,这个时候我们又该如何少写代码让Validation框架来帮我们完成这些基础校验呢?
带着这个疑问,喊一句:翠花,上酸菜。
正文
首先定义我们的Validation的基础类,基础类只有一个字段:errMsg,用于我们校验不通过时候存储我们的提示信息:
@Data
public class ValidationBaseDTO {private String errMsg;
}
然后定义我们的测试对象类,搞一个简单的,这个DTO集成我们的校验基础类
@Data
public class ValidationTestDTO extends ValidationBaseDTO {@NotEmpty(message = "用户名不允许为空!")private String userName;@NotEmpty(message = "用户code不允许为空")private String userCode;private int age;}
然后编写咱们的校验工具类:
public class ValidationUtils{public static <E, T extends ValidationBaseDTO> List<T> validate(Validator validator, E e) {return validate(validator, e, Default.class);}public static <E, T extends ValidationBaseDTO> List<T> validate(Validator validator, E e, Class<?> groupClass) {Set<ConstraintViolation<E>> set = validator.validate(e, groupClass);if (CollectionUtils.isEmpty(set)) {return null;}Map<String, List<ConstraintViolation<E>>> resultGroup = set.stream().collect(Collectors.groupingBy(item -> item.getPropertyPath().toString().substring(0, item.getPropertyPath().toString().indexOf("."))));return resultGroup.entrySet().stream().map(item -> {T targetObject = (T)item.getValue().get(0).getLeafBean();String errMsg = String.join("|", item.getValue().stream().map(ConstraintViolation<E>::getMessage).collect(Collectors.toList()));targetObject.setErrMsg(errMsg);return targetObject;}).collect(Collectors.toList());}}
校验工具类有了,那还得搞一个测试用的Controller
@Slf4j
@RestController
@RequestMapping(value = "validation")
@AllArgsConstructor
public class ValidationTestController {private final Validator validator;@RequestMapping(value = "validationTest")public CommonResult<List<ValidationTestDTO>> validationTest() {// 例如我们通过Excel导入的数据有两条,属性全为空ValidationTestDTO validationTestDTO1 = new ValidationTestDTO();ValidationTestDTO validationTestDTO2 = new ValidationTestDTO();List<ValidationTestDTO> validationTestDTOList = new ArrayList<>();validationTestDTOList.add(validationTestDTO1);validationTestDTOList.add(validationTestDTO2);// 校验结果如果为空,则说明全部通过,如果不为空,则说明有的校验没有通过List<ValidationTestDTO> resultList = ValidationUtils.validate(validator, new ValidatedList<>(validationTestDTOList));return ResultUtil.success(resultList);}
}
这里不得不提的就是,Validator 在Spring框架里面是有被实例化的,且由Sping框架管理,我们直接注入就可以了
差点忘了,如果需要校验List,我们还需要自定义一个ValidationList类,如下:
public class ValidatedList<E> implements List<E>, Serializable {public ValidatedList(List<E> eList){this.list = eList;}@Validprivate List<E> list = new LinkedList<>();@Overridepublic int size() {return list.size();}@Overridepublic boolean isEmpty() {return list.isEmpty();}@Overridepublic boolean contains(Object o) {return list.contains(o);}@Overridepublic Iterator<E> iterator() {return list.iterator();}@Overridepublic Object[] toArray() {return list.toArray();}@Overridepublic <T> T[] toArray(T[] a) {return list.toArray(a);}@Overridepublic boolean add(E e) {return list.add(e);}@Overridepublic boolean remove(Object o) {return list.remove(o);}@Overridepublic boolean containsAll(Collection<?> c) {return list.containsAll(c);}@Overridepublic boolean addAll(Collection<? extends E> c) {return list.addAll(c);}@Overridepublic boolean addAll(int index, Collection<? extends E> c) {return list.addAll(index, c);}@Overridepublic boolean removeAll(Collection<?> c) {return list.removeAll(c);}@Overridepublic boolean retainAll(Collection<?> c) {return list.retainAll(c);}@Overridepublic void clear() {list.clear();}@Overridepublic E get(int index) {return list.get(index);}@Overridepublic E set(int index, E element) {return list.set(index, element);}@Overridepublic void add(int index, E element) {list.add(index, element);}@Overridepublic E remove(int index) {return list.remove(index);}@Overridepublic int indexOf(Object o) {return list.indexOf(o);}@Overridepublic int lastIndexOf(Object o) {return list.lastIndexOf(o);}@Overridepublic ListIterator<E> listIterator() {return list.listIterator();}@Overridepublic ListIterator<E> listIterator(int index) {return list.listIterator(index);}@Overridepublic List<E> subList(int fromIndex, int toIndex) {return list.subList(fromIndex, toIndex);}
}
如果这个类不定义,直接传入我们请求参数的List,那是无效的;
启动,看效果:
添加图片注释,不超过 140 字(可选)