对列表的元素进行验证

摘要:开发中经常需要校验用户提交的值是否满足要求,@Valid可用于方法参数、返回值等的验证,但是对于参数为列表时无效,此处记录几种对列表进行验证的方法

@Valid 注解通常用于验证单个对象的字段,而不是整个列表。仅添加@Valid注解不适用List原因:@Valid注解适用于Java Bean上的验证,原生的List位于Java Util, 并不属于Java Bean, 所以验证器@Valid不生效。

当需要对列表中的每个对象进行验证时,通常可使用如下途径实现:

1. 手动校验

通过手动验证,可以自己定制复杂的校验逻辑,但处理的数据多的时候工作量非常大。下例中遍历列表,对其中的元素逐个手动校验价格和库存。

java 复制代码
@ApiOperation("后台批量更新商品")
@PostMapping("/admin/product/batchUpdate")
public ApiRestResponse batchUpdateProduct(@Valid @RequestBody List<UpdateProductReq> updateProductReqList) {
    for (int i = 0; i < updateProductReqList.size() ; i++) {
        UpdateProductReq updateProductReq = updateProductReqList.get(i);
        // 方法一: 手动校验
        if (updateProductReq.getPrice() < 1) {
            throw new ImoocMallException(ImoocMallExceptionEnum.PRICE_TO_LOW);
        }
        if (updateProductReq.getStock() > 10000) {
            throw new ImoocMallException(ImoocMallExceptionEnum.STOCK_TO_MANY);
        }
        Product product = new Product();
        BeanUtils.copyProperties(updateProductReq, product);
        productService.update(product);
    }
    return ApiRestResponse.success();
}

2. 自定义列表

自定义列表自身具有将列表中属性验证的能力,例如下例中的ValidList,其仅仅是对List的接口简单封装了层。

ValidList类实现了 List 接口,并在内部包含了一个使用了@Valid注解的 List 对象。这个类的作用是具有校验能力的列表,可以对其中的元素进行验证;在使用ValidList类的时候,如果给定的元素类上有相应的校验规则,则可以通过@Valid注解来触发校验。

除了包含校验能力的列表属性外,ValidList 类还实现了List接口中的所有方法,这些方法会直接调用内部的 list 对象的对应方法来完成相应的操作。这意味着可以像使用普通列表一样使用ValidList,同时也能够享受到对元素的校验功能。 总之,ValidList 类的设计在需要对列表元素进行校验的情况下,方便地使用Bean Validation API提供的功能,同时也保留了普通列表的所有操作特性。

java 复制代码
@ApiOperation("后台批量更新商品,validList验证")
@PostMapping("/admin/product/batchUpdate2")
public ApiRestResponse batchUpdateProduct2(@Valid @RequestBody ValidList<UpdateProductReq> updateProductReqList) {
    for (int i = 0; i < updateProductReqList.size() ; i++) {
        UpdateProductReq updateProductReq = updateProductReqList.get(i);
        // 方法二: 自定义列表
        Product product = new Product();
        BeanUtils.copyProperties(updateProductReq, product);
        productService.update(product);
    }

    return ApiRestResponse.success();
}

***ValidList.java***
package com.imooc.mall.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import javax.validation.Valid;
import java.util.List;

/**
 * 具有检验能力的List
 */
public class ValidList<E> implements List<E> {
    @Valid
    private List<E> list;

    public ValidList() {
        this.list = new ArrayList<E>();
    }

    public ValidList(List<E> list) {
        this.list = list;
    }

    public List<E> getList() {
        return list;
    }

    public void setList(List<E> list) {
        this.list = list;
    }

    @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<E> iterator() {
        return list.iterator();
    }

    @Override
    public Object[] toArray() {
        return list.toArray();
    }

    @Override
    public <T> 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<E> listIterator() {
        return list.listIterator();
    }

    @Override
    public ListIterator<E> listIterator(int index) {
        return list.listIterator(index);
    }

    @Override
    public List<E> subList(int fromIndex, int toIndex) {
        return list.subList(fromIndex, toIndex);
    }
}

3. 使用@Validated 注解

@Validated是Spring提供的相对于@Valid注解的功能的增强版:支持集合内元素验证;支持分组验证。

java 复制代码
// Controller中添加注解:
@Validated

@ApiOperation("后台批量更新商品,@Validated验证")
@PostMapping("/admin/product/batchUpdate3")
public ApiRestResponse batchUpdateProduct3(@Valid @RequestBody List<UpdateProductReq> updateProductReqList) {
    for (int i = 0; i < updateProductReqList.size() ; i++) {
        UpdateProductReq updateProductReq = updateProductReqList.get(i);
        // 方法三: @validated
        Product product = new Product();
        BeanUtils.copyProperties(updateProductReq, product);
        productService.update(product);
    }
    return ApiRestResponse.success();
}

使用 @Validated所对应抛出的异常需要特殊处理下,在全局异常处理类中添加;

java 复制代码
@ExceptionHandler
@ResponseBody
@ResponseStatus(HttpStatus.OK)
public ApiRestResponse handle(ConstraintViolationException exception) {
    // 从exception中获取到所有的验证违规信息,并存储在一个Set集合中
    Set<ConstraintViolation<?>> violations = exception.getConstraintViolations();
    // 创建一个StringBuilder对象,用于构建错误信息的字符串
    StringBuilder builder = new StringBuilder();
    for (ConstraintViolation<?> violation: violations) {  // 遍历违规信息的集合
        builder.append(violation.getMessage());  // 将当前违规信息的错误消息追加到builder中
        break;   // 在处理完第一条违规信息后跳出循环
    } // 传入错误码和错误消息构建一个响应对象,并将其作为方法的返回值
    return ApiRestResponse.error(ImoocMallExceptionEnum.REQUEST_PARAM_ERROR.getCode(), builder.toString());
}
相关推荐
float_六七1 小时前
IntelliJ IDEA双击Ctrl的妙用
java·ide·intellij-idea
能摆一天是一天3 小时前
JAVA stream().flatMap()
java·windows
CodeCraft Studio3 小时前
PDF处理控件Aspose.PDF教程:使用 Python 将 PDF 转换为 Base64
开发语言·python·pdf·base64·aspose·aspose.pdf
零点零一3 小时前
VS+QT的编程开发工作:关于QT VS tools的使用 qt的官方帮助
开发语言·qt
颜如玉3 小时前
🤲🏻🤲🏻🤲🏻临时重定向一定要能重定向🤲🏻🤲🏻🤲🏻
java·http·源码
程序员的世界你不懂5 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
星空寻流年5 小时前
设计模式第一章(建造者模式)
java·设计模式·建造者模式
lingchen19065 小时前
MATLAB的数值计算(三)曲线拟合与插值
开发语言·matlab
gb42152875 小时前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
一朵梨花压海棠go6 小时前
html+js实现表格本地筛选
开发语言·javascript·html·ecmascript