Bean Validation 主页 声明“Bean Validation 是一种 Java 规范,它......在 Java SE 中运行,但集成在 Java EE(6 和 7)中。”这篇文章演示了在 Java EE 容器之外使用 Java Bean 验证 参考实现 ( Hibernate Validator )。这篇文章中的示例基于 Hibernate Validator 5.1.3 Final ,可以从 http://hibernate.org/validator/downloads 下载。
“ Getting Started with Hibernate Validator ”指出 Hibernate Validator 需要 统一表达式语言 ( JSR 341 ) 和 上下文和依赖注入 ( CDI / JSR 346 ) 的实现。这些规范的实现在现代 Java EE 兼容容器(应用程序服务器)中可用,但在 Java SE 环境中使用 Hibernate Validator 需要采购和使用单独的实现。
“Hibernate Validator 入门”页面提供了 Maven XML,可用于识别 表达式语言 API 的依赖项(我使用的是 表达式语言 3.0 API )、 表达式语言实现 (我使用的是 表达式语言实现 2.2.6 )和 Hibernate Validator CDI 便携式扩展 (我使用的是 Hibernate Validator Portable Extension 5.1.3 Final )。我还使用 Bean Validation API 1.1.0 Final 、 JBoss Logging 3.3.0 Final 和 ClassMate 1.2.0 来构建和运行我的示例。
为本文中演示的 bean 验证示例定义了三个 Java 类。其中一个类
Car.java
改编自“
Hibernate Validator 入门
”页面上提供的示例,其代码清单如下所示。
汽车.java
package dustin.examples;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
/**
-
Example adapted from "Getting Started with Hibernate Validator"
-
(http://hibernate.org/validator/documentation/getting-started/).
*/
public class Car
{
@NotNull
private String manufacturer;
@NotNull
@Size(min = 2, max = 14)
private String licensePlate;
@Min(2)
private int seatCount;
public Car(final String manufacturer, final String licencePlate, final int seatCount)
{
this.manufacturer = manufacturer;
this.licensePlate = licencePlate;
this.seatCount = seatCount;
}
public String getManufacturer()
{
return manufacturer;
}
public String getLicensePlate()
{
return licensePlate;
}
public int getSeatCount()
{
return seatCount;
}
@Override
public String toString()
{
return "Car{" +
"manufacturer='" + manufacturer + ''' +
", licensePlate='" + licensePlate + ''' +
", seatCount=" + seatCount +
'}';
}
}
这篇文章的示例中使用的另一个类是在
Garage.java
中定义的,主要是
Car
的多个实例的包装器。它的主要目的是帮助说明 Hibernate Bean Validator 支持的递归验证。
车库.java
package dustin.examples;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
/**
-
Example adapted from "Getting Started with Hibernate Validator"
-
(http://hibernate.org/validator/documentation/getting-started/).
*/
public class Car
{
@NotNull
private String manufacturer;
@NotNull
@Size(min = 2, max = 14)
private String licensePlate;
@Min(2)
private int seatCount;
public Car(final String manufacturer, final String licencePlate, final int seatCount)
{
this.manufacturer = manufacturer;
this.licensePlate = licencePlate;
this.seatCount = seatCount;
}
public String getManufacturer()
{
return manufacturer;
}
public String getLicensePlate()
{
return licensePlate;
}
public int getSeatCount()
{
return seatCount;
}
@Override
public String toString()
{
return "Car{" +
"manufacturer='" + manufacturer + ''' +
", licensePlate='" + licensePlate + ''' +
", seatCount=" + seatCount +
'}';
}
}
上面的
Garage
代码清单使用
@Valid 注释
来指示类持有的
Car
实例也应该被验证(“验证级联”)。
本文示例中使用的最后一个 Java 类是将实际执行两个 bean 验证注释类
Car
和
Garage
的验证的类。此类的清单如下所示。
HibernateValidatorDemonstration.java
package dustin.examples;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
/**
-
Example adapted from "Getting Started with Hibernate Validator"
-
(http://hibernate.org/validator/documentation/getting-started/).
*/
public class Car
{
@NotNull
private String manufacturer;
@NotNull
@Size(min = 2, max = 14)
private String licensePlate;
@Min(2)
private int seatCount;
public Car(final String manufacturer, final String licencePlate, final int seatCount)
{
this.manufacturer = manufacturer;
this.licensePlate = licencePlate;
this.seatCount = seatCount;
}
public String getManufacturer()
{
return manufacturer;
}
public String getLicensePlate()
{
return licensePlate;
}
public int getSeatCount()
{
return seatCount;
}
@Override
public String toString()
{
return "Car{" +
"manufacturer='" + manufacturer + ''' +
", licensePlate='" + licensePlate + ''' +
", seatCount=" + seatCount +
'}';
}
}
上面的代码包含对 javax.validation.Validator.validate(T, Class<?>) 的多次调用,这些调用展示了对其实例正在被验证的类的注释的有效性。几个示例验证对象的单个验证违规,一个示例验证对象的多个验证违规,最后一个示例演示成功的级联违规检测。
HibernateValidatorDemonstration
类有一个
main(String[])
函数,可以在 Java SE 环境中执行(假设必要的 JAR 位于运行时类路径中)。运行上述演示类的输出如下所示:
package dustin.examples;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
/**
-
Example adapted from "Getting Started with Hibernate Validator"
-
(http://hibernate.org/validator/documentation/getting-started/).
*/
public class Car
{
@NotNull
private String manufacturer;
@NotNull
@Size(min = 2, max = 14)
private String licensePlate;
@Min(2)
private int seatCount;
public Car(final String manufacturer, final String licencePlate, final int seatCount)
{
this.manufacturer = manufacturer;
this.licensePlate = licencePlate;
this.seatCount = seatCount;
}
public String getManufacturer()
{
return manufacturer;
}
public String getLicensePlate()
{
return licensePlate;
}
public int getSeatCount()
{
return seatCount;
}
@Override
public String toString()
{
return "Car{" +
"manufacturer='" + manufacturer + ''' +
", licensePlate='" + licensePlate + ''' +
", seatCount=" + seatCount +
'}';
}
}
结论
这篇文章展示了 Hibernate Bean Validator(Bean 验证规范的参考实现)可以在 Java SE 环境中执行。作为演示的一部分,还讨论和演示了与 Bean Validation 规范和 Hibernate Bean Validator 实现相关的一些基本概念。
其他资源