创建一个校验bean,里面定义List、Page、Info、Insert、Update、Delete等分组校验类型
1. 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
2. 创建分组校验bean
import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.*;
public class ValidGroup<E> implements Serializable, List<E> {
private static final long serialVersionUID = -6962864446731919547L;
public interface List {
}
public interface Page {
}
public interface Info {
}
public interface Insert {
}
public interface InsertBatch {
}
public interface Update {
}
public interface UpdateBatch {
}
public interface Delete {
}
public interface DeleteBatch {
}
public interface WorkFlow {
}
public interface ListAsAttribute {
}
public interface ListObject {
}
@Valid
@Size(min = 1, message = "对象列表不能为空", groups = {ListObject.class, InsertBatch.class, UpdateBatch.class, DeleteBatch.class})
private java.util.List<E> list = new ArrayList<>();
@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 java.util.List<E> subList(int fromIndex, int toIndex) {
return list.subList(fromIndex, toIndex);
}
public java.util.List<E> getList() {
return list;
}
public void setList(java.util.List<E> list) {
this.list = list;
}
}
3. 给bean加校验注解,并定义自己的message信息提示
import com.kcx.equipment.common.ValidGroup;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
@ApiModel(value="巡查管理—巡查记录")
@Data
public class InspectionRecordQueryDTO implements Serializable {
@ApiModelProperty(value = "整改部门id")
@NotBlank(message = "整改部门不能为空",groups = {ValidGroup.List.class})
private String rectifyDepartmentId;
@NotBlank(message = "巡查开始日期不能为空", groups = {ValidGroup.Page.class,ValidGroup.List.class})
private String beginTime;
@NotBlank(message = "巡查开始日期不能为空", groups = {ValidGroup.Page.class,ValidGroup.List.class})
private String endTime;
private static final long serialVersionUID = 1L;
}
4. 业务方法参数上使用@Validated注解,并在value中给出group接口,标记当前校验是哪个组
@ApiOperation(value = "分页查询巡查记录", notes = "分页查询", httpMethod = "GET")
@GetMapping("/queryPage")
public ResponseVo<PageInfo<InspectionRecord>> queryPage(@Validated(value = {ValidGroup.Page.class}) InspectionRecordQueryDTO dto) {
// 登录用户信息
UserInfo userInfo = UserContext.getUserInfo();
return inspectionRecordService.queryPage(dto);
}