JSR-303简介
JSR-303 是 Java EE 6 中的一项子规范,叫做 Bean Validation,官方参考实现是hibernate Validator。
此实现与 Hibernate ORM 没有任何关系。 JSR 303 用于对 Java  Bean 中的字段的值进行验证。 Spring MVC 3.x 之中也大力支持 JSR-303,可以在控制器中对表单提交的数据方便地验证。
注:可以使用注解的方式进行验证
maven 依赖
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.4.0.Final</version>
</dependency>
<dependency>
    <groupId>javax.el</groupId>
    <artifactId>el-api</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.el</artifactId>
    <version>2.2.4</version>
</dependency>
JSR 303 基本的校验规则
Bean Validation 中的 constraint
1. Bean Validation 中内置的 constraint
| Constraint | 详细信息 | 
|---|---|
| @Null | 被注释的元素必须为 null | 
| @NotNull | 被注释的元素必须不为 null | 
| @AssertTrue | 被注释的元素必须为 true | 
| @AssertFalse | 被注释的元素必须为 false | 
| @Min(value) | 被注释的元素必须是一个数字,其值必须大于等于指定的最小值 | 
| @Max(value) | 被注释的元素必须是一个数字,其值必须小于等于指定的最大值 | 
| @DecimalMin(value) | 被注释的元素必须是一个数字,其值必须大于等于指定的最小值 | 
| @DecimalMax(value) | 被注释的元素必须是一个数字,其值必须小于等于指定的最大值 | 
| @Size(max, min) | 被注释的元素的大小必须在指定的范围内 | 
| @Digits (integer, fraction) | 被注释的元素必须是一个数字,其值必须在可接受的范围内 | 
| @Past | 被注释的元素必须是一个过去的日期 | 
| @Future | 被注释的元素必须是一个将来的日期 | 
| @Pattern(value) | 被注释的元素必须符合指定的正则表达式 | 
2. Hibernate Validator 附加的 constraint
| Constraint | 详细信息 | 
|---|---|
| 被注释的元素必须是电子邮箱地址 | |
| @Length | 被注释的字符串的大小必须在指定的范围内 | 
| @NotEmpty | 被注释的字符串的必须非空 | 
| @Range | 被注释的元素必须在合适的范围内 | 
一个 constraint 通常由 annotation 和相应的 constraint validator 组成,它们是一对多的关系。也就是说可以有多个 constraint validator 对应一个 annotation。在运行时,Bean Validation 框架本身会根据被注释元素的类型来选择合适的 constraint validator 对数据进行验证。
有些时候,在用户的应用中需要一些更复杂的 constraint。Bean Validation 提供扩展 constraint 的机制。可以通过两种方法去实现,一种是组合现有的 constraint 来生成一个更复杂的 constraint,另外一种是开发一个全新的 constraint。
简单示例
Order
package com.bytebeats.codelab.bean.validation.model;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
public class Order {
    @NotNull(message = "orderId必须不为null")
    @Size(min = 10, max = 10)
    private String orderId;
    @NotEmpty(message = "customer须不为空")
    private String customer;
    @Email(message = "email必须是一个电子信箱地址")
    private String email;
    @NotEmpty(message = "address必须不为空")
    private String address;
    @NotNull(message = "status必须不为 null")
    private String status;
    @NotNull(message = "createDate必须不为 null")
    private Date createDate;
    
    //getter and setter
}
测试类:
//调用JSR303验证工具,校验参数
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Order order = new Order();
        order.setOrderId("12345");
        order.setCustomer("ricky");
        order.setEmail("ricky");
        order.setAddress("北京市朝阳区");
        order.setStatus("DELV");
        order.setCreateDate(new Date());
        Set<ConstraintViolation<Order>> violations = validator.validate(order);
        for(ConstraintViolation<Order> violation: violations) {
            System.out.println(violation.getPropertyPath()+"\t"+ violation.getMessage());
        }
运行结果输出:
orderId     个数必须在10和10之间
email   email必须是一个电子信箱地址
定制化的 constraint
参考资料
Getting started with Hibernate Validator
JSR 303 - Bean Validation 介绍及最佳实践










网友评论