@Valid对List中的实体类的属性校验的两种解决方案

1、实体类注解

@Builder
@Data
public class User{

    
    @NotBlank(message = "姓名不能为空!")
    private String name;

    @NotBlank(message = "性别不能为空!")
    private String gender;
}

2、请求报文中如果有“list”属性,解决方案如下:

请求报文:

{
	"list": [
		{
			"name": "Tom",
			"gender": "MAN"
		
		}
	]
}

第一步:定义一个接收参数的类:

@Data
public class ListRequestDTO {

    @Valid
    private List list;
}

第二步:Controller用@Valid注解的ListRequestDTO接收参数即可

 public RespResult saveUser(@RequestBody @Valid ListRequestDTO userList) {
        ...
    }

3、请求报文中如果没有“list”属性,解决方案如下:

请求报文:

 [
	{
		"name": "Tom",
		"gender": "MAN"
		
	}
]

第一步:自定义一个ValidList基础List

public class ValidList implements List {

    private List list;

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

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

    public List getList() {
        return list;
    }

    public void setList(List 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 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.contains(c);
    }

    @Override
    public boolean addAll(Collection c) {
        return list.addAll(c);
    }

    @Override
    public boolean addAll(int index, Collection 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);
    }
}

第二步:Controller用@Valid注解的ValidList接收参数即可


public RespResult saveUser(@RequestBody @Valid ValidList userList) {
        ...
    }

如果此博客有帮助到你的话,请不要吝啬你的手指!点赞+转发让更多的人可以看到,帮助到更多的同学们,谢谢喽!

你可能感兴趣的:(Java基础)