1. 問題描述:@NotNull 參數校驗失效java
2. 代碼:app
controller 接口 @PostMapping("/saveSpu") public SHttpResponse<Boolean> saveSpu(@Valid @RequestBody ProductSaveDto productSaveDto) { SResponse<Boolean> re = productSpuService.saveSpu(productSaveDto); return SHttpResponse.transform(re); } 對象 public class ProductSaveDto { @NotNull private ProductSpuEntity spuEntity; ... } public class ProductSpuEntity { @NotNull(message = "分類id不能爲空") private Integer categoryId; }
3. 解決方法:在被校驗的屬性類上增長註解@Vail.net
修改後代碼 public class ProductSaveDto { @Valid @NotNull private ProductSpuEntity spuEntity; ... }