校验实体对象必传参数是否为空

单对象校验

实体类UserInfo.java

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;

@Data
public class UserInfo implements Serializable {
    @ApiModelProperty(value = "标题", required = true)
    @NotEmpty(message = "名称不能为空")
    private String title;
    
    @ApiModelProperty(value = "创建时间", required = true)
    @NotNull(message = "创建时间不能为空")
    private Date createTime;
}

其中message用于提示语内容

控制层 UserApi.java

  import javax.validation.Valid;


  @ApiOperation(value = "测试接口")
  @ApiImplicitParam(name = "data", value = "数据", required = true, dataType = "UserInfo", paramType = "body")
  @PostMapping
  public ResultBean<Boolean> businessStatus(@RequestBody @Valid UserInfo data) {
        //业务操作
        return ResultBean.build();
    }

特别注意:@RequestBody@Valid 顺序必须如上所示!

全局异常拦截 GlobalExceptionHandler.java


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.management.MBeanException;

/**
 * 全局异常处理
 * 作用在所有注解了@RequestMapping的控制器的方法上
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResultBean paramException(MethodArgumentNotValidException ex) {
        logger.error(ex.getMessage(), ex);
        return ResultBean.build(110, ex.getBindingResult().getFieldError().getDefaultMessage());
    }

}

特别注意:ex.getBindingResult().getFieldError().getDefaultMessage()) 获取之前绑定的message`内容

集合对象校验(List<Object>

添加ValidList.java

import javax.validation.Valid;
import java.util.*;

public class ValidList<E> implements List<E> {

    @Valid
    private List<E> list = new ArrayList<>();

    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.contains(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);
    }
}

控制层 UserApi.java

  import javax.validation.Valid;


  @ApiOperation(value = "测试接口")
  @ApiImplicitParam(name = "data", value = "数据", required = true, dataType = "UserInfo", allowMultiple = true, paramType = "body")
  @PostMapping
  public ResultBean<Boolean> businessStatus(@RequestBody @Valid ValidList<UserInfo> data) {
        //业务操作
        return ResultBean.build();
    }

特别注意:@RequestBody@Valid 顺序必须如上所示!

posted @ 2022-01-06 15:58  _不正  阅读(108)  评论(0编辑  收藏  举报