Java BeanValidation

来源:互联网 发布:同步带轮生成软件 编辑:程序博客网 时间:2024/05/16 13:59

SR(Java Specification Requests,Java规范提案) 303 – Bean Validation 是一个数据验证的规范,2009 年 11 月确定最终方案。2009 年 12 月 Java EE 6 发布,Bean Validation 作为一个重要特性被包含其中。

hibernate Validator 是 Bean Validation 的参考实现 . Hibernate Validator 提供了 JSR 303 规范中所有内置 constraint 的实现,除此之外还有一些附加的 constraint。

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详细信息@Email被注释的元素必须是电子邮箱地址@Length被注释的字符串的大小必须在指定的范围内@NotEmpty被注释的字符串的必须非空@Range被注释的元素必须在合适的范围内有些时候,在用户的应用中需要一些更复杂的 constraint。Bean Validation 提供扩展 constraint 的机制。可以通过两种方法去实现,一种是组合现有的 constraint 来生成一个更复杂的 constraint,另外一种是开发一个全新的 constraint。

示例

所需Jar包:validation-api-1.0.0.GA.jar、hibernate-validator-4.2.0.Final.jar、slf4j-api-1.6.1.jar。

Order类:

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. package com.zzj.beanvalidation;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.validation.Valid;  
  6. import javax.validation.constraints.NotNull;  
  7. import javax.validation.constraints.Size;  
  8.   
  9. public class Order {  
  10.     @NotNull(message = "订单号不能为空 ")   
  11.     @Size(max = 11, message = "订单号长度不能超过11")  
  12.     private String PlatformOrderID;  
  13.     @Valid // 级联验证  
  14.     private Receiver Receiver;  
  15.     @Valid // 级联验证  
  16.     private List<Product> ProductList;  
  17.   
  18.     public String getPlatformOrderID() {  
  19.         return PlatformOrderID;  
  20.     }  
  21.   
  22.     public void setPlatformOrderID(String platformOrderID) {  
  23.         PlatformOrderID = platformOrderID;  
  24.     }  
  25.   
  26.     public Receiver getReceiver() {  
  27.         return Receiver;  
  28.     }  
  29.   
  30.     public void setReceiver(Receiver receiver) {  
  31.         Receiver = receiver;  
  32.     }  
  33.   
  34.     public List<Product> getProductList() {  
  35.         return ProductList;  
  36.     }  
  37.   
  38.     public void setProductList(List<Product> productList) {  
  39.         ProductList = productList;  
  40.     }  
  41.   
  42. }  
Receiver类:
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. package com.zzj.beanvalidation;  
  2.   
  3. import javax.validation.constraints.NotNull;  
  4.   
  5. import org.hibernate.validator.constraints.NotEmpty;  
  6.   
  7. public class Receiver {  
  8.     @NotEmpty(message = "国家不能空")  
  9.     private String Country;  
  10.     private String CountryCode;  
  11.     @NotNull(message = "邮箱不能为空")  
  12.     @org.hibernate.validator.constraints.Email(message = "邮箱格式不正确")  
  13.     private String Email;  
  14.   
  15.     public String getCountry() {  
  16.         return Country;  
  17.     }  
  18.   
  19.     public void setCountry(String country) {  
  20.         Country = country;  
  21.     }  
  22.   
  23.     public String getCountryCode() {  
  24.         return CountryCode;  
  25.     }  
  26.   
  27.     public void setCountryCode(String countryCode) {  
  28.         CountryCode = countryCode;  
  29.     }  
  30.   
  31.     public String getEmail() {  
  32.         return Email;  
  33.     }  
  34.   
  35.     public void setEmail(String email) {  
  36.         Email = email;  
  37.     }  
  38.       
  39. }  
Product类:
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. package com.zzj.beanvalidation;  
  2.   
  3. import javax.validation.constraints.DecimalMin;  
  4. import javax.validation.constraints.NotNull;  
  5. import javax.validation.constraints.Pattern;  
  6.   
  7. public class Product {  
  8.     @NotNull(message = "英文品名不能为空")  
  9.     private String CustomsName;  
  10.     private String CustomsCnName;  
  11.     @DecimalMin(value = "0", message = "申报价值不能小于0")  
  12.     private String DeclareValue;  
  13.     @Pattern(regexp = "\\d", message = "数量不合法"// 正则验证  
  14.     private String Quantity;  
  15.   
  16.     public String getCustomsName() {  
  17.         return CustomsName;  
  18.     }  
  19.   
  20.     public void setCustomsName(String customsName) {  
  21.         CustomsName = customsName;  
  22.     }  
  23.   
  24.     public String getCustomsCnName() {  
  25.         return CustomsCnName;  
  26.     }  
  27.   
  28.     public void setCustomsCnName(String customsCnName) {  
  29.         CustomsCnName = customsCnName;  
  30.     }  
  31.   
  32.     public String getDeclareValue() {  
  33.         return DeclareValue;  
  34.     }  
  35.   
  36.     public void setDeclareValue(String declareValue) {  
  37.         DeclareValue = declareValue;  
  38.     }  
  39.   
  40.     public String getQuantity() {  
  41.         return Quantity;  
  42.     }  
  43.   
  44.     public void setQuantity(String quantity) {  
  45.         Quantity = quantity;  
  46.     }  
  47.   
  48. }  
测试:
[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. package com.zzj.beanvalidation;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Set;  
  6.   
  7. import javax.validation.ConstraintViolation;  
  8. import javax.validation.Validation;  
  9. import javax.validation.Validator;  
  10. import javax.validation.ValidatorFactory;  
  11.   
  12. public class Test {  
  13.     public static void main(String[] args) throws Exception {  
  14.         ValidatorFactory vf = Validation.buildDefaultValidatorFactory();  
  15.         Validator validator = vf.getValidator();  
  16.         Order order = new Order();  
  17.         order.setPlatformOrderID("111111111111111"); // 不合法的订单长度  
  18.           
  19.         Receiver receiver = new Receiver();  
  20.         receiver.setEmail("dddd"); // 不合法的邮箱  
  21.         order.setReceiver(receiver);  
  22.           
  23.         List<Product> products = new ArrayList<Product>();  
  24.         Product product = new Product();  
  25.         product.setQuantity("1.0"); // 不合法的数量  
  26.         product.setDeclareValue("-1"); // 不合法的申报价值  
  27.         products.add(product);  
  28.         order.setProductList(products);  
  29.           
  30.         Set<ConstraintViolation<Order>> validate = validator.validate(order);  
  31.         for (ConstraintViolation<Order> cv : validate) {  
  32.             System.out.println(cv.getMessage() + "(" + cv.getPropertyPath() + ")"  
  33.                     + "-->" + cv.getInvalidValue());  
  34.         }  
  35.     }  
  36. }  
结果:
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. 申报价值不能小于0(ProductList[0].DeclareValue)-->-1  
  2. 数量不合法(ProductList[0].Quantity)-->1.0  
  3. 订单号长度不能超过11(PlatformOrderID)-->111111111111111  
  4. 国家不能空(Receiver.Country)-->null  
  5. 邮箱格式不正确(Receiver.Email)-->dddd  
  6. 英文品名不能为空(ProductList[0].CustomsName)-->null  
0 0
原创粉丝点击