我們可以寫一個集合實現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());
}
}
**錯誤資訊詳情如下: **簡單明瞭