解决The annotation @NotBlank is disallowed for this data type异常

导读:本篇文章讲解 解决The annotation @NotBlank is disallowed for this data type异常,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

JDK1.8环境下 springboot2集成hibernet-validation 6.0.7.FINAL作参数校验时,用

javax.validation.constraints.NotBlank的注解出现了,The annotation @NotBlank is disallowed for this data type编译异常, 

import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;

@Data
public class DemoModel {

    @NotEmpty(message = "用户名称不能为空")
    private String userName;

    @NotBlank(message = "年龄不能为空")
    @Pattern(regexp = "^[0-9]{1,2}$" ,message =  "年龄不正确")
    private String age;

    /**
     * 如果是空,则不校验,如果不为空,则校验
     */
    @Pattern(regexp="^[0-9]{4}-[0-9]{2}-[0-9]{2}$",message="出生日期格式不正确,比如:2000-1-20")
    private String birthday;

}

已经有的配置:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
.....

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

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.7.Final</version>
        </dependency>

Idea有如下Waringing:

Warning:java: 来自注释处理程序 ‘org.hibernate.validator.ap.ConstraintValidationProcessor’ 的受支持 source 版本 ‘RELEASE_6’ 低于 -source ‘1.8’

在idea中搜索ConstraintValidationProcessor源码

解决The annotation @NotBlank is disallowed for this data type异常

 注解实现类确实有JDK1.6的限制, 而我当前时JDK1.8

,再一看此类归属

hibernate-validator-annotation-processor 居然是4.1.0.Final版本,将其升级到6.0.7.Final,编译问题即可解决!

解决The annotation @NotBlank is disallowed for this data type异常

 增加org.hibernate:hibernate-validator-annotation-processor:6.0.7.Final pom配置

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
.....

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

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.7.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator-annotation-processor</artifactId>
            <version>6.0.7.Final</version>
        </dependency>

参考资料:

springboot使用hibernate validator校验

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/9920.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!