【Bean校验】

关键概念澄清:bean validation是规范,通过JSR定义。   Hibernate validation是具体的实现。

参考:

  https://beanvalidation.org/2.0/  规范官网,介绍规范演进,JSR1.0->1.1->2.0 定义和实现方(Hibernate validation)              

  https://www.baeldung.com/javax-validation   快速上手样例

  https://www.cnblogs.com/yourbatman/p/13595278.html  

一、Bean Validation 规范演进:

JSR 303: V1.0

JSR 349: V1.1

JSR 380: V2.0  因2018年3月,Oracle 决定把 JavaEE 移交给开源组织Eclipse基金会(Jakarta EE),所以后续POM中由javax变更为jakarta 

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.0.0.GA</version>
</dependency>

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>

<dependency>
    <groupId>jakarta.validation</groupId>
    <artifactId>jakarta.validation-api</artifactId>
    <version>2.0.1</version>
</dependency>

https://beanvalidation.org/2.0/ 官网:

 


 

二、 Hibernate validation

参考上章节,作为Bean Validation规范的实现,实际使用中仅引用当前JAR即可。

快速上手参考:https://www.baeldung.com/javax-validation,以下内容摘抄关键部分附上。

 POM:必须添加EL的仓库,否则会提示报错

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

<!-- JSR 380 supports variable interpolation, allowing expressions inside the violation messages.
To parse these expressions, we'll add the javax.el dependency from GlassFish, that contains an implementation of the Expression Language specification: -->       
<dependency>
    <groupId>org.glassfish</groupId>
    <artifactId>javax.el</artifactId>
    <version>3.0.0</version>
</dependency>   

bean定义

public class User {

    @NotNull(message = "Name cannot be null")
    private String name;

    @AssertTrue
    private boolean working;

    @Size(min = 10, max = 200, message 
      = "About Me must be between 10 and 200 characters")
    private String aboutMe;

    @Min(value = 18, message = "Age should not be less than 18")
    @Max(value = 150, message = "Age should not be greater than 150")
    private int age;

    @Email(message = "Email should be valid")
    private String email;

    // standard setters and getters 
}

校验

User user = new User();
user.setWorking(true);
user.setAboutMe("Its all about me!");
user.setAge(10);

ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
Validator validator = validatorFactory.getValidator();
Set<ConstraintViolation<User>> violations = validator.validate(user);
for (ConstraintViolation<User> violation : violations) {
    System.err.println(violation.getMessage());
}

结果:

Name cannot be null
Age should not be less than 18

开发过程:

1、Some frameworks — such as Spring — have simple ways to trigger the validation process by just using annotations. This is mainly so that we don't have to interact with the programmatic validation API.  // Spring集成使用更便捷

     Now let's go the manual route and set things up programmatically:

2、To validate a bean, we first need a Validator object, which is built using a ValidatorFactory.

3、Now that we have a Validator, we can validate our bean by passing it to the validate method.

     Any violations of the constraints defined in the User object will be returned as a Set:

    By iterating over the violations, we can get all the violation messages using the getMessage method:   

posted @ 2021-08-19 20:59  飞翔在天  阅读(56)  评论(0编辑  收藏  举报