我们可以写一个集合实现list,代码如下,在controller类上面不用加@Validated注解,在controller使用自定义ValidableList集合,对list进行封装,就可以对scheduleDtoList做参数校验处理。
@Api(tags = "xxxx 管理")
@RestController
@RequestMapping("aaa/indicatorDeviceCategoryDetailsController")
public class IndicatorDeviceCategoryDetailsController {
@PostMapping("/batchUpdateIndicatorDeviceCategorys")
@ApiOperation("根据参数dto-批量修改告警设备或异常设备的数据值")
public ResponseVo batchUpdateIndicatorDeviceCategoryDetails(@RequestBody @Validated ValidableList<IndicatorDeviceScheduleDto> scheduleDtoList){
return indicatorDeviceCategoryDetailsService.batchUpdateIndicatorDeviceCategoryDetails(scheduleDtoList);
}
}
优点,异常清晰明了
import lombok.Data;
import org.jetbrains.annotations.NotNull;
import javax.validation.Valid;
import java.util.*;
import java.util.function.UnaryOperator;
/**
* @author psd 用于校验指定的List集合里面的数据
*/
@Data
public class ValidableList<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);
}
@NotNull
@Override
public Iterator<E> iterator() {
return list.iterator();
}
@NotNull
@Override
public Object[] toArray() {
return list.toArray();
}
@NotNull
@Override
public <T> T[] toArray(@NotNull 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(@NotNull Collection<?> c) {
return list.containsAll(c);
}
@Override
public boolean addAll(@NotNull Collection<? extends E> c) {
return list.addAll(c);
}
@Override
public boolean addAll(int index, @NotNull Collection<? extends E> c) {
return list.addAll(index, c);
}
@Override
public boolean removeAll(@NotNull Collection<?> c) {
return list.removeAll(c);
}
@Override
public boolean retainAll(@NotNull Collection<?> c) {
return list.retainAll(c);
}
@Override
public void replaceAll(UnaryOperator<E> operator) {
List.super.replaceAll(operator);
}
@Override
public void sort(Comparator<? super E> c) {
List.super.sort(c);
}
@Override
public void 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);
}
@NotNull
@Override
public ListIterator<E> listIterator() {
return list.listIterator();
}
@NotNull
@Override
public ListIterator<E> listIterator(int index) {
return list.listIterator(index);
}
@NotNull
@Override
public List<E> subList(int fromIndex, int toIndex) {
return list.subList(fromIndex, toIndex);
}
@Override
public Spliterator<E> spliterator() {
return List.super.spliterator();
}
}
温馨提示,要写下面统一异常处理,要不然拦截不了异常
import com.dx.major.common.bean.ResponseVo;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import lombok.extern.slf4j.Slf4j;
/**
* @author psc 统一参数校验异常处理类
*/
@Slf4j
@ControllerAdvice
public class ValidatedExceptionHandler {
/**
* 处理@Validated 参数校验异常
*
* @param exception
* 异常类型
* @return 异常信息
*/
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseVo exceptionHandler(MethodArgumentNotValidException exception) {
BindingResult result = exception.getBindingResult();
StringBuilder builder = new StringBuilder();
if (result.hasErrors()) {
result.getAllErrors().forEach(p -> {
FieldError fieldError = (FieldError) p;
log.warn("Bad Request Parameters: dto entity [{}],field [{}],message [{}]", fieldError.getObjectName(), fieldError.getField(),
fieldError.getDefaultMessage());
builder.append(fieldError.getDefaultMessage());
});
}
return ResponseVo.paramError(builder.toString());
}
}
**错误信息详情如下: **简单明了