非Controller控制层参数校验怎么办

  1. 参数校验背景

    • 通常在Spring Boot中,参数校验是在Controller控制层完成的。
    • 文章探讨了在非Controller控制层进行参数校验的方法。
  2. Validator接口

    • Spring提供了JSR-303的一个变种@Validated。
    • Validator接口定义了用于执行验证的方法,主要方法包括:
      • validate(T object, Class<?>... groups):验证对象是否符合约束条件。
      • validateProperty(T object, String propertyName, Class<?>... groups):验证对象的特定属性是否符合约束条件。
      • validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups):验证给定属性值是否符合约束条件,无需创建对象实例。
  3. Validator接口的使用

    • 依赖Spring容器 :
      • 通过Spring容器注入Validator接口的实现类。
      • 示例代码展示了如何在Spring Boot中使用Validator进行参数校验。
    • 不依赖Spring容器 :
      • JSR 303是一种规范,不依赖Spring容器,可以在任何Java项目中使用。
      • 通过ValidatorFactory获取Validator接口的实现。
      • 示例代码展示了如何在非Spring项目中使用Validator进行参数校验。
  4. 示例代码

    • UserBean类 :

      java 复制代码
      @Data
      public class UserBean {
          @NotBlank
          private String username;
      
          @Min(value = 18)
          private Integer age;
      }
    • Spring Boot中的Validator使用 :

      java 复制代码
      @SpringBootTest
      class SpringbootPracticeApplicationTests {
          @Resource
          private Validator validator;
      
          @Test
          public void testValidator(){
              UserBean userBean = new UserBean();
              userBean.setAge(17);
              userBean.setUsername(null);
      
              Set<ConstraintViolation<UserBean>> constraintViolations = validator.validate(userBean);
              System.out.println("validate 校验对象属性:");
              System.out.println(constraintViolations);
      
              constraintViolations = validator.validateProperty(userBean, "age");
              System.out.println("validateProperty校验age属性是否合规:");
              System.out.println(constraintViolations);
      
              constraintViolations = validator.validateValue(UserBean.class , "age", 14 );
              System.out.println("validateValue校验age属性的值是否合规:");
              System.out.println(constraintViolations);
          }
      }
    • 非Spring项目中的Validator使用 :

      java 复制代码
      public class ValidationUtils {
          public static Validator getValidator(){
              ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
              return factory.getValidator();
          }
      }
      
      public class ValidatorTest {
          public static void main(String[] args) {
              Validator validator = ValidationUtils.getValidator();
              UserBean userBean = new UserBean();
              userBean.setAge(17);
              userBean.setUsername(null);
      
              Set<ConstraintViolation<UserBean>> constraintViolations = validator.validate(userBean);
              System.out.println("validate 校验对象属性:");
              System.out.println(constraintViolations);
      
              constraintViolations = validator.validateProperty(userBean, "age");
              System.out.println("validateProperty校验age属性是否合规:");
              System.out.println(constraintViolations);
      
              constraintViolations = validator.validateValue(UserBean.class , "age", 14 );
              System.out.println("validateValue校验age属性的值是否合规:");
              System.out.println(constraintViolations);
          }
      }

测试结果

  • 校验对象属性 :
    • 显示违反约束条件的字段及其提示信息。
  • 校验特定属性 :
    • 仅显示特定属性违反约束条件的提示信息。
  • 校验属性值 :
    • 显示给定属性值是否符合约束条件的提示信息。

总结

  • 依赖Spring容器的方法和不依赖Spring容器的方法。
  • 建议使用不依赖Spring容器的方法,可以将获取Validator的代码封装成工具类,统一管理校验方法。
相关推荐
考虑考虑11 小时前
记一次@Async生产事故
spring boot·后端·spring
Mr.Java.21 小时前
JetCache自定义序列化实战,Kryo/Protostuff编解码器一步接入
java·spring boot·spring
CoderYanger1 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第八弹):MyBatis Generator
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
jaysee-sjc2 天前
【黑马点评】Day02:优惠券秒杀与分布式锁
java·分布式·nginx·spring·node.js·tomcat·maven
CoderYanger2 天前
Java EE 进阶:10. MyBatis-Plus使用(第一弹)
java·spring boot·后端·sql·spring·java-ee·mybatis
CoderYanger2 天前
Java EE 进阶:9.MyBatis 操作数据库(进阶)(第七弹):图书管理系统(强制登录)
java·数据库·spring boot·spring·java-ee·tomcat·mybatis
陈如水3 天前
Spring AI Alibaba 在多 Agent 场景下的实践
java·人工智能·spring
Resean02233 天前
Spring 源码分析(六)循环依赖与三级缓存
java·spring·缓存
行百里er3 天前
【Spring Insight 技术分享】跨服务调用链路是怎么串起来的?
后端·spring·开源
Forget the Dream4 天前
Spring AI 2.0 系列(三):ChatClient 与 ChatModel:Spring AI 2.0 的两层职责
java·人工智能·spring·springai