赞
踩
今天开发接口,遇到请求参数固定为List<CommonConfigBean>类型的,如下。发现参数校验失效。
- @RequestMapping(method = RequestMethod.POST)
- public void insertCommonConfig(@RequestBody @Validated List<CommonConfigBean> list){
- int result=commonConfigService.insertCommonConfig(list);
- //更新缓存池
- if(result>0) {
- updateCache(list);
- log.info("refresh the configCache");
- }
- }
- @Getter
- @Setter
- @ToString
- public class CommonConfigBean {
- /**主键id*/
- private Integer id;
- /**参数名称*/
- @Length(max = 30)
- @NotEmpty
- private String itemName;
- /**参数值*/
- @Length(max = 1000)
- @NotEmpty
- private String itemValue;
- /**备注*/
- @Length(max = 50)
- private String memo;
- }
新建一个类,把List<SaveVideoReq>作为成员变量, 然后用@Valid注解在List上。
- public class CommonConfigBeanWrapper{
-
- @Valid
- private List<CommonConfigBean> req;
-
- }
3. 解决方案二:
但是,由于某种原因,前端只会传[{“name”:“name”,“sex”:“sex”,“type":"视频"},{...}]。因此,请求对象是固定的,不能用方案一处理。此时,可以参考网上的一个方法,创建一个类,实现List接口,并添加List<E> list = new ArrayList<>();或者LinkedList作为成员变量,如下:
- package com.dahuatech.peopasg.infrastructure.bean;
-
- import lombok.Data;
-
- import javax.validation.Valid;
- import java.util.*;
-
- @Data
- public class ValidList<E> implements List<E> {
-
- @Valid
- private List<E> 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<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);
- }
- }
然后,控制器的参数列表就变成了如下。此时,就可以正常从bingdingResult获取校验结果了。
- @RequestMapping(method = RequestMethod.POST)
- public void insertCommonConfig(@RequestBody @Validated ValidList<CommonConfigBean> list){
- int result=commonConfigService.insertCommonConfig(list);
- //更新缓存池
- if(result>0) {
- updateCache(list);
- log.info("refresh the configCache");
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。