Springboot使用JSR 303对控制器控制层校验及服务服务层AOP校验使用消息资源文件对消息国际化

  

  

导入JSR 303的包,hibernate有效的包

        & lt; dependency>   & lt; groupId> org.hibernate.validator   & lt; artifactId> hibernate-validator   & lt; version> 6.0.5.Final   & lt;/dependency>   & lt; dependency>   & lt; groupId> javax.validation   & lt; artifactId> validation-api   & lt; version> 2.0.0.Final   & lt;/dependency>      

<强> springboot配置

  

资源/应用程序。yml消息资源文件国际化处理配置

  春天

:
  ,消息:
  ,,::base, todo #资源文件基地。属性和todo.properties,多个用逗号隔开

  

,,,编码:utf - 8 #必须指定解析编码,否则中文乱码

  

在springboot启动类里面配置

        @SpringBootApplication   公共类的应用程序扩展WebMvcConfigurerAdapter {   @ value (" $ {spring.messages.basename} ")   私人字符串:;   公共静态void main (String [] args) {   SpringApplication.run (Application.class, args);   }   @ bean   @Primary   公共MessageSource MessageSource () {   ResourceBundleMessageSource ResourceBundleMessageSource=new ResourceBundleMessageSource ();   resourceBundleMessageSource.setUseCodeAsDefaultMessage(假);   resourceBundleMessageSource.setDefaultEncoding (“utf - 8”);//重复定义   resourceBundleMessageSource.setBasenames (basename.split (", "));   返回resourceBundleMessageSource;   }   @ bean   @Primary   公共LocalValidatorFactoryBean validator () {   LocalValidatorFactoryBean validatorFactoryBean=new LocalValidatorFactoryBean ();   validatorFactoryBean.setProviderClass (HibernateValidator.class);   validatorFactoryBean.setValidationMessageSource (messageSource ());   返回validatorFactoryBean;   }   @Override   公开验证器getValidator () {   返回验证器();   }/* *   *方法级别的单个参数验证开启   */@ bean   公共MethodValidationPostProcessor MethodValidationPostProcessor () {   返回新MethodValidationPostProcessor ();   }   }      

我们对于校验参数通过不了抛出的异常进行处理,是通过统一异常捕捉。

        @ControllerAdvice   @ component   公开课BindValidExceptionHandler {   @ResponseStatus (value=https://www.yisu.com/zixun/HttpStatus.OK)   @ExceptionHandler (ConstraintViolationException.class)   公共@ResponseBody   味精handleConstraintViolationException (ConstraintViolationException e) {   .iterator字符串messageTemplate=e.getConstraintViolations () () . next () .getMessageTemplate ();   返回Msg.error (messageTemplate);   }   @ResponseStatus(值=HttpStatus.OK)   @ExceptionHandler (BindException.class)   公共@ResponseBody   味精handleBindException (BindException e) {   BindingResult BindingResult=e.getBindingResult ();   字符串className=bindingResult.getTarget () .getClass () . getname ();   .iterator FieldError下=bindingResult.getFieldErrors () () . next ();   字符串字段名=next.getField ();   字符串defaultMessage=next.getDefaultMessage ();   如果(Pattern.compile (IllegalArgumentException:不枚举).matcher (defaultMessage); ()) {   匹配器匹配器=Pattern.compile(“价值”(* & # 63;)”).matcher (defaultMessage);   如果(matcher.find ()) {   defaultMessage="找不到枚举类型”【+ matcher.group (1) +“】”;   }   }   返回Msg.error (defaultMessage);   }   @ResponseStatus (value=https://www.yisu.com/zixun/HttpStatus.OK)   @ExceptionHandler (ValidError.class)   公共@ResponseBody   味精handleValidError (ValidError e) {   返回Msg.error (e.getMessage ());   }   }      

资源/base.propertie   

creatorId=创建者id不能为小于}{价值。
  

  

modifierId=修改者id不能为小于}{价值。
  

  

资源/todo.properties   

todo.privateId。min=私有id不能为小于}{价值。

  

在豆字段上使用注解,其中组中的C和S接口是指控制器和服务的叫法简称,里面分别有插入接口,更新接口等等,都是自定义约定的东西。

  

     /* *   *私有id,是代表项目任务/非项目任务/风险/问题/评审待办问题等多张表的外键   */@Min (value=https://www.yisu.com/zixun/1,消息=" {todo.privateId。分钟}”、组={C.Insert。类,C.Update。类,S.Insert。类,S.Update.class})   私人长privateId;/* *   *创建者id   */@Min (value=https://www.yisu.com/zixun/1,消息=" {creatorId}”,组={S.Insert.class})   私人长creatorId;      控制器控制层验证      @Validated   @RestController   @RequestMapping(“备忘录”)   公开课TodoController {   @ autowired   私人TodoService TodoService;   @GetMapping (“getVo”)   公共味精getVo (   @Min (value=https://www.yisu.com/zixun/1,消息="待办身份证不能小于1。”)   @RequestParam (=false, defaultValue=" https://www.yisu.com/zixun/0 ")   长id   ){   返回this.todoService.getVo (id);   }   @PostMapping(“添加”)   公共味精添加(@Validated ({C.Insert.class}) Todo Todo) {   返回this.todoService.add (todo);   }   }

Springboot使用JSR 303对控制器控制层校验及服务服务层AOP校验使用消息资源文件对消息国际化