@NotBlank不生效的问题分析

hibernate-validator中@NotBlank不生效的问题分析

背景介绍

由于某些历史原因,使用的SpringBoot版本不能超过2.3.9.RELEASE

为了进行参数的校验。需要使用validation框架,从springboot-2.3.x开始,spring-boot-starter-web模块中不再引入 spring-boot-starter-validation,所以需要额外手动引入validation依赖,而 2.3之前的版本只需要引入 web 依赖。

在Spring Boot 项目的 pom.xml 文件中,添加以下依赖

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

@Valid@Validated

@Valid来源于JDK,JSR-380。标注位置在方法上, 字段上, 构造函数上, 参数上, 类型上。包路径:javax.validation.Valid

提示验证框架进行嵌套验证;能配合嵌套验证注解@Valid进行嵌套验证。

@Validated是是Spring对@Valid 的一次封装,是Spring提供的校验机制。标注位置在范围类上、接口(包括注释类型)上,枚举声明上。包路径:org.springframework.validation.annotation.Validated

不能提示验证框架进行嵌套验证;能配合嵌套验证注解@Valid进行嵌套验证。

常用校验注解说明

注解名注解效果注解作用类型
@NotNull被注解的元素不能为null,可以为空字符串any type
@NotBlank验证注解的元素值不为空(不为null、去除首位空格后长度为0)String
@NotEmpty用在集合类上,不能为null,并且长度必须大于0CharSequence,List,Map,Array
@Size被注释的元素的大小必须在指定的范围内。CharSequence,List,Map,Array
@Pattern支持,正则的验证。Java regular expression
@Max被注释的元素必须是一个数字,其值必须小于等于指定的最大值BigDecimal
BigInteger
byte, short, int, long, and their respective wrappers
@Min被注释的元素必须是一个数字,其值必须大于等于指定的最小值BigDecimal
BigInteger
byte, short, int, long, and their respective wrappers

详细使用

百度去吧

问题现象

javax.validation.constraints.NotBlankjavax.validation.constraints.NotEmpty验证的时候无效,但是org.hibernate.validator.constraints.NotBlankorg.hibernate.validator.constraints.NotEmpty有效。在这个版本的hibernate-validator上,org.hibernate.validator.constraints.NotBlankorg.hibernate.validator.constraints.NotEmpty已经标记为废弃了。这是不合理的。

分析思路

Bean Validation的情况梳理

先去了解Bean Validation的由来

Bean Validation是JSR,规范提案,目前迭代的版本:

Bean Validation 1.0:JSR 303 => https://www.jcp.org/en/jsr/detail?id=303

Bean Validation 1.1:JSR 349 => https://www.jcp.org/en/jsr/detail?id=349

Bean Validation 2.0:JSR 380 => https://www.jcp.org/en/jsr/detail?id=380

Bean Validation的实现

通过Hibernate Validator进行实现规范。在实现上面规范的时候,除了规范约定的约束,还会增加一些常用的Constraint的实现。

javax.validation.constraints是JDK中的约束,而org.hibernate.validator.constraints就是实现的时候补充的一些约束。在迭代的时候,一些注解会先出现在hibernate中,后面规范升级的时候再补充到JDK中。这样就需要去hibernate中检查是否有对应的注解校验器。在org.hibernate.validator.internal.constraintvalidators中,bv是基础校验器,hv是hibernate的校验器。

在bv和hv中都存在校验器,说明,上述问题中不同两个包的NotBlank注解都应该可以生效的。在本地springboot中的启动,javax.validation.constraints.NotBlank是可以生效的,而生产环境中却不可以。

Bean Validation的主页:http://beanvalidation.org

Bean Validation的参考实现:https://github.com/hibernate/hibernate-validator

猜想原因

不同的Servlet容器是不是对这个Bean Validation有什么关系?

检查wildfly中 modules中关于Validation的内容。采用的wildfly版本是8(版本真的旧),安装位置是/opt,那validation的路径/opt/wildfly/modules/system/layers/base/javax/validation/api/main/。找到JAR validation-api-1.1.0.Final.jar。发现,规范用的是1.1。检查了一下NotBlank的规范版本,是2.0。validation-api-1.1.0.Final.jar中并没有javax.validation.constraints.NotBlank

结论

Bean Validation跟Servlet容器(Tomcat或者Wildfly)的版本是有关的。

说明这个版本的wildfly不支持javax.validation.constraints.NotBlank

解决方案

  1. 由于不能升级wildfly,则统一使用org.hibernate.validator.constraints.NotBlank,同时需要梳理出Bean Validation 2.0中增加的哪些注解,都是在不是通过javax.validation.constraints使用的。
    1. @Email @NotEmpty, @NotBlank, @Positive, @PositiveOrZero, @Negative, @NegativeOrZero, @PastOrPresent and @FutureOrPresent

补充

Bean Validation - What’s new in 2.0

  • Support for validating container elements by annotating type arguments of parameterized types, e.g. List<@Positive Integer> positiveNumbers (see Container element constraints); this also includes:
    • More flexible cascaded validation of collection types; e.g. values and keys of maps can be validated now: Map<@Valid CustomerType, @Valid Customer> customersByType
    • Support for java.util.Optional
    • Support for the property types declared by JavaFX
    • Support for custom container types by plugging in additional value extractors (see Value extractor definition)
  • Support for the new date/time data types for @Past and @Future (see Built-in Constraint definitions); fine-grained control over the current time and time zone used for validation (see Implementation of temporal constraint validators)
  • New built-in constraints: @Email, @NotEmpty, @NotBlank, @Positive, @PositiveOrZero, @Negative, @NegativeOrZero, @PastOrPresent and @FutureOrPresent (see Built-in Constraint definitions)
  • All built-in constraints are marked as repeatable now
  • Parameter names are retrieved using reflection (see Naming parameters)
  • ConstraintValidator#initialize() is a default method (see Constraint validation implementation)
  • The namespace for Bean Validation XML descriptors has been changed to http://xmlns.jcp.org/xml/ns/validation/configuration for META-INF/validation.xml and http://xmlns.jcp.org/xml/ns/validation/mapping for constraint mapping files (see XML configuration: META-INF/validation.xml)
  • 30
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
回答: 当使用实体类中的@NotBlank注解时,需要确保使用的是Java自带的@NotBlank注解,而不是Hibernate下的注解。因此,在实体类中导入的@NotBlank注解应该使用javax.validation.constraints.NotBlank而不是org.hibernate.validator.constraints.NotBlank。同时,还需要确保在pom.xml文件中添加了javax.validation和hibernate-validator的依赖。如果依然存在@NotBlank注解不生效问题,可能是由于缺少配置或其他因素导致的,可以进一步检查代码逻辑、配置文件和相关依赖是否正确使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [@NotBlank注解不生效解决方法](https://blog.csdn.net/numbbe/article/details/118711371)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [java中,如何解决@NotBlank不生效问题 @NotBlank @NotEmpty不生效,以及对象嵌套问题...](https://blog.csdn.net/j610152753/article/details/127201199)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

顧棟

若对你有帮助,望对作者鼓励一下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值