用法一:在body请求的controller里面校验对象。
@PostMapping(value="/combineByOrderGoodsCalculate")
public ResponseResult<List<OrderGoods>> combineByOrderGoodsCalculate(@Valid @RequestBody CombineParma combineParma){
return ResponseResult.success(combineApp.doCombine(combineParma.getUserInfo(),combineParma.getOrderGoodsList()));
}
用法二:在对象里面校验子对象或者集合
@RequiredArgsConstructor
@Getter
public class CombineParma {
@NotNull
@Valid
private final UserInfo userInfo;
@NotNull
@Size(min=1,max=99)
@Valid
private final List<OrderGoods> orderGoodsList;
}
注意:@Valid 是表示给集合或者对象要进行校验。如果一个对象的子对象不加@Valid 就会导致子对象的校验失效。
@Getter
@RequiredArgsConstructor //有参构造
public class UserInfo {
@NotNull
@Range(min = 1,max=9999999)
private final Long userId;
@NotNull
@Range(min = 1,max=5)
private final Integer userLevel;
}
注意 基本类型不用加@Valid ,例如
@RequestMapping(value="/loadGoodsSkuSpec" ,method={RequestMethod.POST,RequestMethod.GET})
@ResponseBody
public ResponseResult<GoodsSkuSpec> loadGoodsSkuSpec( @NotNull(message = "goodsId不能为空") @Positive(message = "goodsId必须为正整数") Long goodsId){
return ResponseResult.success(goodsApp.loadGoodsSkuSpec(goodsId));
}
网友评论