validation 后台校验
实体类字段上注解
@Null // The annotated element must be {@code null}
@NotNull // The annotated element must not be {@code null}
@NotEmpty // The annotated element must not be {@code null} nor empty
controller层注解
@RequestMapping("/save")
public R save(@Validated @RequestBody BrandEntity brand, BindingResult bindingResult){
// brandService.save(brand);
if (bindingResult.hasErrors()) {
Map<String,String> map = new HashMap<>();
bindingResult.getFieldErrors().forEach((x) -> {
// 获取提示信息
String defaultMessage = x.getDefaultMessage();
//获取错误的属性的名字
String field = x.getField();
map.put(field,defaultMessage);
});
return R.error(400,"提交数据不合法").put("data",map);
}
return R.ok();
}