JSR303校验 JSR303校验 统一异常处理 编写异常处理类: 系统错误码 分组校验 自定义校验 校验总结 JSR303校验 @RequestMapping("/save") //@RequiresPermissions("product:brand:save") public R save(@Validated(AddGroup.class) @RequestBody BrandEntity brand , BindingResult result ){ if (result.hasErrors()){ Map<String,String> map = new HashMap<>(); //1.获取校验错误信息 result.getFieldError().forEach((item)->{ //FieldError获取到错误提示 String message = item.getDefaultLocaleMassage(); //获取到错误的属性的名字 String field = item.getField(); map.put(field,message); }); return R.error(404,"提交的数据不合法").put("data",map); }else { brandService.save(brand); } return R.ok(); 统一异常处理 编写异常处理类: /* GulimallExceptionControllerAdvice.java*/ /** * 集中处理所有异常 */ // 日志记录 @Slf4j //@ResponseBody //@ControllerAdvice(basePackages = "com.atguigu.gulimall.product.controller") // 上面两个注解的合体 @RestControllerAdvice(basePackages = "com.atguigu.gulimall.product.controller") public class GulimallExceptionControllerAdvice { /*精确处理某类异常:MethodArgumentNotValidException*/ // 指定可以处理什么异常 @ExceptionHandler(value = MethodArgumentNotValidException