• java使用validator检验bean


    介绍,首先大家常用的hibernate-validator,它是对JSR-303/JSR-349标准的实现,然后spring为了给开发者提供便捷集成了hibernate-validator,默认在springMVC模块下。

    依赖

    首先加上web模块

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

      可以查看它的子依赖,就可以看到web模块默认使用了hibernate-validator:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     4   <modelVersion>4.0.0</modelVersion>
     5   <parent>
     6     <groupId>org.springframework.boot</groupId>
     7     <artifactId>spring-boot-starters</artifactId>
     8     <version>2.1.6.RELEASE</version>
     9   </parent>
    10   <groupId>org.springframework.boot</groupId>
    11   <artifactId>spring-boot-starter-web</artifactId>
    12   <version>2.1.6.RELEASE</version>
    13   <name>Spring Boot Web Starter</name>
    14   <description>Starter for building web, including RESTful, applications using Spring
    15         MVC. Uses Tomcat as the default embedded container</description>
    16   <url>https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-web</url>
    17   <organization>
    18     <name>Pivotal Software, Inc.</name>
    19     <url>https://spring.io</url>
    20   </organization>
    21   <licenses>
    22     <license>
    23       <name>Apache License, Version 2.0</name>
    24       <url>https://www.apache.org/licenses/LICENSE-2.0</url>
    25     </license>
    26   </licenses>
    27   <developers>
    28     <developer>
    29       <name>Pivotal</name>
    30       <email>info@pivotal.io</email>
    31       <organization>Pivotal Software, Inc.</organization>
    32       <organizationUrl>https://www.spring.io</organizationUrl>
    33     </developer>
    34   </developers>
    35   <scm>
    36     <connection>scm:git:git://github.com/spring-projects/spring-boot.git/spring-boot-starters/spring-boot-starter-web</connection>
    37     <developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git/spring-boot-starters/spring-boot-starter-web</developerConnection>
    38     <url>https://github.com/spring-projects/spring-boot/spring-boot-starters/spring-boot-starter-web</url>
    39   </scm>
    40   <issueManagement>
    41     <system>Github</system>
    42     <url>https://github.com/spring-projects/spring-boot/issues</url>
    43   </issueManagement>
    44   <dependencies>
    45     <dependency>
    46       <groupId>org.springframework.boot</groupId>
    47       <artifactId>spring-boot-starter</artifactId>
    48       <version>2.1.6.RELEASE</version>
    49       <scope>compile</scope>
    50     </dependency>
    51     <dependency>
    52       <groupId>org.springframework.boot</groupId>
    53       <artifactId>spring-boot-starter-json</artifactId>
    54       <version>2.1.6.RELEASE</version>
    55       <scope>compile</scope>
    56     </dependency>
    57     <dependency>
    58       <groupId>org.springframework.boot</groupId>
    59       <artifactId>spring-boot-starter-tomcat</artifactId>
    60       <version>2.1.6.RELEASE</version>
    61       <scope>compile</scope>
    62     </dependency>
    63     <dependency>
    64       <groupId>org.hibernate.validator</groupId>
    65       <artifactId>hibernate-validator</artifactId>
    66       <version>6.0.17.Final</version>
    67       <scope>compile</scope>
    68     </dependency>
    69     <dependency>
    70       <groupId>org.springframework</groupId>
    71       <artifactId>spring-web</artifactId>
    72       <version>5.1.8.RELEASE</version>
    73       <scope>compile</scope>
    74     </dependency>
    75     <dependency>
    76       <groupId>org.springframework</groupId>
    77       <artifactId>spring-webmvc</artifactId>
    78       <version>5.1.8.RELEASE</version>
    79       <scope>compile</scope>
    80     </dependency>
    81   </dependencies>
    82 </project>
    View Code

    注解jsr

    hibernate-validator注解

    @NotBlank(message =) 验证字符串非null,且长度必须大于0

    @Email 被注释的元素必须是电子邮箱地址

    @Length(min=,max=) 被注释的字符串的大小必须在指定的范围内

    @NotEmpty 被注释的字符串的必须非空

    @Range(min=,max=,message=) 被注释的元素必须在合适的范围内

  • 相关阅读:
    控制语句
    (小程序)接收用户键盘输入
    运算符
    java doc形成注释文档
    linux中rz、sz命令,zip、unzip命令,sqlite3基本操作
    hash应用-加随机盐保存密码
    非对称/对称加密算法,hash算法
    自签名的应用--数字货币
    HTTPS原理
    Django中app的model相互引用问题
  • 原文地址:https://www.cnblogs.com/xining/p/11282814.html
Copyright © 2020-2023  润新知