我们的接口是一个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
extends ValidationBaseDTO> List validate(Validator validator, E e) { - return validate(validator, e, Default.class);
- }
-
- public static
extends ValidationBaseDTO> List validate(Validator validator, E e, Class> groupClass) { - Set
> set = validator.validate(e, groupClass); - if (CollectionUtils.isEmpty(set)) {
- return null;
- }
- Map
>> 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
::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
> validationTest() {
- // 例如我们通过Excel导入的数据有两条,属性全为空
- ValidationTestDTO validationTestDTO1 = new ValidationTestDTO();
- ValidationTestDTO validationTestDTO2 = new ValidationTestDTO();
- List
validationTestDTOList = new ArrayList<>(); - validationTestDTOList.add(validationTestDTO1);
- validationTestDTOList.add(validationTestDTO2);
-
- // 校验结果如果为空,则说明全部通过,如果不为空,则说明有的校验没有通过
- List
resultList = ValidationUtils.validate(validator, new ValidatedList<>(validationTestDTOList)); - return ResultUtil.success(resultList);
- }
- }
这里不得不提的就是,Validator 在Spring框架里面是有被实例化的,且由Sping框架管理,我们直接注入就可以了
差点忘了,如果需要校验List,我们还需要自定义一个ValidationList类,如下:
- public class ValidatedList
implements List, Serializable { -
- public ValidatedList(List
eList) { - this.list = eList;
- }
- @Valid
- private List
list = new LinkedList<>(); -
- @Override
- public int size() {
- return list.size();
- }
-
- @Override
- public boolean isEmpty() {
- return list.isEmpty();
- }
-
- @Override
- public boolean contains(Object o) {
- return list.contains(o);
- }
-
- @Override
- public Iterator
iterator() { - return list.iterator();
- }
-
- @Override
- public Object[] toArray() {
- return list.toArray();
- }
-
- @Override
- public
T[] toArray(T[] a) { - return list.toArray(a);
- }
-
- @Override
- public boolean add(E e) {
- return list.add(e);
- }
-
- @Override
- public boolean remove(Object o) {
- return list.remove(o);
- }
-
- @Override
- public boolean containsAll(Collection> c) {
- return list.containsAll(c);
- }
-
- @Override
- public boolean addAll(Collection extends E> c) {
- return list.addAll(c);
- }
-
- @Override
- public boolean addAll(int index, Collection extends E> c) {
- return list.addAll(index, c);
- }
-
- @Override
- public boolean removeAll(Collection> c) {
- return list.removeAll(c);
- }
-
- @Override
- public boolean retainAll(Collection> c) {
- return list.retainAll(c);
- }
-
- @Override
- public void clear() {
- list.clear();
- }
-
- @Override
- public E get(int index) {
- return list.get(index);
- }
-
- @Override
- public E set(int index, E element) {
- return list.set(index, element);
- }
-
- @Override
- public void add(int index, E element) {
- list.add(index, element);
- }
-
- @Override
- public E remove(int index) {
- return list.remove(index);
- }
-
- @Override
- public int indexOf(Object o) {
- return list.indexOf(o);
- }
-
- @Override
- public int lastIndexOf(Object o) {
- return list.lastIndexOf(o);
- }
-
- @Override
- public ListIterator
listIterator() { - return list.listIterator();
- }
-
- @Override
- public ListIterator
listIterator(int index) { - return list.listIterator(index);
- }
-
- @Override
- public List
subList(int fromIndex, int toIndex) { - return list.subList(fromIndex, toIndex);
- }
- }
如果这个类不定义,直接传入我们请求参数的List,那是无效的;
启动,看效果:
添加图片注释,不超过 140 字(可选)