JSR 303 – Bean Validation 是一个数据验证的规范。
在任何时候,当你要处理一个应用程序的业务逻辑时,必须要考虑数据校验,确保输入进来的数据从语 义上来讲是正确的。在通常的情况下,应用程序是分层的,不同的层由不同的开发人员来完成。很多时候同样的数据验证逻辑会出现在不同的层,这样就会导致代码冗余、不利于维护等问题。使用Bean Validation,将验证逻辑与相应的域模型进行绑定,能够很好的避免发生这样的问题。
Bean Validation 为 JavaBean 验证定义了相应的元数据模型和 API。缺省的元数据是 Java Annotations。在应用程序中,通过使用 Bean Validation 或是你自己定义的 constraint,例如@NotNull
,@Max
,@Size
, 就可以确保数据模型(JavaBean)的正确性。Bean Validation 是一个运行时的数据验证框架,在验证之后验证的错误信息会被马上返回。
Hibernate Validator 是 Bean Validation 的参考实现 . Hibernate Validator 提供了 JSR 303 规范中所有内置 constraint 的实现,除此之外还有一些附加的 constraint。借助Hibernate Validator,可以很好的进行参数验证。
例子如下(摘自参考文档2的内容):
1、maven依赖:
<dependency> <groupId>com.alibaba.external</groupId> <artifactId>sourceforge.hibernate.validator</artifactId> <version>4.0.2.GA</version> </dependency> <dependency> <groupId>com.alibaba.external</groupId> <artifactId>java.validation.api</artifactId> <version>1.0.0.GA</version> </dependency> <dependency> <groupId>com.alibaba.external</groupId> <artifactId>org.slf4j.slf4j-api</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>com.alibaba.external</groupId> <artifactId>org.slf4j.slf4j-log4j12</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>com.alibaba.external</groupId> <artifactId>jakarta.log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>com.alibaba.external</groupId> <artifactId>sourceforge.spring</artifactId> <version>2.5.6</version> </dependency>
2、要校验的Java Bean
package com.mycompany; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class Car { @NotNull private String manufacturer; @NotNull @Size(min = 2, max = 14) private String licensePlate; @Min(2) private int seatCount; public Car(String manufacturer, String licencePlate, int seatCount) { this.manufacturer = manufacturer; this.licensePlate = licencePlate; this.seatCount = seatCount; } //getters and setters ... }3、进行的校验
import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import org.junit.BeforeClass; import org.junit.Test; public class CarTest { private static Validator validator; @BeforeClass public static void setUp() { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); validator = factory.getValidator(); } @Test public void manufacturerIsNull() { Car car = new Car(null, "DD-AB-123", 4); Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car); assertEquals(1, constraintViolations.size()); assertEquals("may not be null", constraintViolations.iterator().next().getMessage()); } @Test public void licensePlateTooShort() { Car car = new Car("Morris", "D", 4); Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car); assertEquals(1, constraintViolations.size()); assertEquals("size must be between 2 and 14", constraintViolations.iterator().next().getMessage()); } }
更详细的使用,请看参考文档2.
参考文档:
1、JSR 303:http://jcp.org/en/jsr/summary?id=303
2、HIBERNATE Validator:http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/pdf/hibernate_validator_reference.pdf
3、中文版的:http://docs.jboss.org/hibernate/validator/4.2/reference/zh-CN/html/validator-usingvalidator.html