如下代碼中使用@Valid注解校驗List<CreateWaybillDTO>類型參數(shù)waybillDTOS
@ApiOperation("新建運單")
@PostMapping(value = "createWaybill" + Constant.POST_JSON)
@ApiResponse(code = 200, message = "創(chuàng)建成功", response = ResponseEntity.class)
public ResponseEntity<BaseDTO> createWaybill(@RequestBody @Valid List<CreateWaybillDTO> waybillDTOS) {
return new ResponseEntity(new BaseDTO("200", "創(chuàng)建成功"), HttpStatus.OK);
}
校驗參數(shù)如下
// 運單id
@ApiModelProperty("運單id")
@NotNull
@Min(value = 1, message = "運單id不合法")
private int waybillId;
// 進(jìn)口出口
@ApiModelProperty("進(jìn)口出口")
@NotNull
private int importAndExport;
調(diào)用參數(shù)如下
[
{
"arrivalTime": "string",
"bookingPerson": "string",
"bookingPersonType": "string",
"bookingSpacePrice": "string",
"boxDTOList": [
{
"boxId": 0,
"boxNumber": "string",
"boxType": "string",
"cargoWeight": "string",
"goodsName": "string",
"leadSealNumber": "string"
}
],
"boxRemarks": "string",
"contactAddress": "string",
"contactFullName": "string",
"contactPerson": "string",
"contactPhone": "string",
"contactPoint": "string",
"customerSignsReceipt": "string",
"dangerousGoodsCategory": "string",
"dangerousGoodsIdentification": "string",
"dangerousGoodsNumber": "string",
"dangerousGoodsRemarks": "string",
"departurePort": "string",
"destinationPort": "string",
"emptyBoxGoods": "string",
"financialWithholdingGoods": "string",
"freightCollection": "string",
"imdgr": "string",
"importAndExport": 0,
"internalRemarks": "string",
"packingTime": "string",
"paymentMethod": "string",
"remarks": "string",
"shipName": "string",
"specialRequirements": "string",
"voyage": "string",
"waybillId": 0,
"waybillNo": "string",
"withholdingGoods": "string"
}
]
返回結(jié)果如下
{
"id": null,
"backcode": "200",
"backmsg": "創(chuàng)建成功"
}
我們校驗waybillId最小值是1矮台,傳入?yún)?shù)的值是0洞焙,但是確拿到了校驗通過的結(jié)果
解決方法:
新建一個類實現(xiàn)List接口,并實現(xiàn)對應(yīng)方法,如下
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);
}
}
我們將接口修改成如下
List<CreateWaybillDTO>修改為ValidList<CreateWaybillDTO>我們剛剛新建的實現(xiàn)類
@ApiOperation("新建運單")
@PostMapping(value = "createWaybill" + Constant.POST_JSON)
@ApiResponse(code = 200, message = "創(chuàng)建成功", response = ResponseEntity.class)
public ResponseEntity<BaseDTO> createWaybill(@RequestBody @Valid ValidList<CreateWaybillDTO> waybillDTOS) {
return new ResponseEntity(new BaseDTO("200", "創(chuàng)建成功"), HttpStatus.OK);
}
同樣的參數(shù)調(diào)用這個接口返回結(jié)果如下
{
"timestamp": "2019-05-08",
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.MethodArgumentNotValidException",
"errors": [
{
"codes": [
"Min.createWaybillDTOList.list[0].waybillId",
"Min.createWaybillDTOList.list.waybillId",
"Min.list[0].waybillId",
"Min.list.waybillId",
"Min.waybillId",
"Min.int",
"Min"
],
"arguments": [
{
"codes": [
"createWaybillDTOList.list[0].waybillId",
"list[0].waybillId"
],
"arguments": null,
"defaultMessage": "list[0].waybillId",
"code": "list[0].waybillId"
},
1
],
"defaultMessage": "運單id不合法",
"objectName": "createWaybillDTOList",
"field": "list[0].waybillId",
"rejectedValue": 0,
"bindingFailure": false,
"code": "Min"
}
],
"message": "Validation failed for object='createWaybillDTOList'. Error count: 1",
"path": "/web/WaybillPush/createWaybill.json"
}
問題解決