Notice:本文对Validation api的使用主要是运用了Hibernate-Validator,其中Hibernate-Validator是validation-api的官方扩展,使用的pom如下:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
基于XML的校验
validation-api支持Java Bean和校验脱离,就是可以不在一起,这点方便Api定义者可以Java Bean,但是不添加校验功能,而实现者能够针对Java Bean的定义来决定是否需要校验
例如,
数据模型:
public class OrderBaseModel {
public long id;
public String buyerCbAccoutId;
public String sellerCbAccoutId;
public OrderSatusEnum status;
public String buyerCompanyName;
public String sellerCompanyName;
public String buyerBizPhone;
public String sellerBizPhone;
public List offers = new ArrayList<>();
public String pickType;
public List receiveAddressGroups = new ArrayList<>();
public String message;
public double discountFee;
public double additionalFee;
public String payway;
public long payTimeout;
public Long autoConfirmReceipt;
public long appointedArrivalDate;
....get/set
}
校验逻辑:
<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.1.xsd"
xmlns="http://jboss.org/xml/ns/javax/validation/mapping" version="1.1">
//指定校验模型
<default-package>com.alibaba.cb.api.order.model</default-package>
<bean class="OrderBaseModel" ignore-annotations="true">
//Class级别校验,并设置忽略注解的校验逻辑
<class ignore-annotations="true">
//支持jexl表达式的校验逻辑
<constraint annotation="org.hibernate.validator.constraints.ScriptAssert">
<message>{com.alibaba.cb.api.order.model.ordermodel.receiveAddressGroups}</message>
<element name="lang">jexl</element>
// 这里如果不设置别名,_this指的是当前class的实例对象,可以取到任何一个对象中的属性值进行联动校验
<element name="script">
if(_this.pickType=='push' and size(_this.receiveAddressGroups) > 2){
return false;
}else if(_this.pickType=='pull' and size(_this.receiveAddressGroups) > 10){
return false;
}else if(size(_this.receiveAddressGroups) == 0) {
return false;
}
return true;
</element>
</constraint>
</class>
<field name="buyerCbAccoutId">
<constraint annotation="javax.validation.constraints.NotNull">
<message>{com.alibaba.cb.api.order.model.ordermodel.buyerCbAccoutId}</message>
</constraint>
</field>
<constraint annotation="javax.validation.constraints.Size">
<message>{com.alibaba.cb.api.order.model.ordermodel.buyerCompanyNameSize}</message>
<element name="max">20</element>
</constraint>
</field>
// 支持正则校验逻辑
<field name="sellerBizPhone">
<constraint annotation="javax.validation.constraints.Pattern">
<message>{com.alibaba.cb.api.order.model.ordermodel.sellerBizPhonePattern}</message>
<element name="regexp">^(|[0-9]{1,11})$</element>
</constraint>
</field>
<field name="offers">
<constraint annotation="javax.validation.constraints.Size">
<message>{com.alibaba.cb.api.order.model.ordermodel.offers}</message>
<element name="min">1</element>
</constraint>
</field>
<field name="pickType">
<constraint annotation="javax.validation.constraints.NotNull" >
<message>{com.alibaba.cb.api.order.model.ordermodel.pickType}</message>
</constraint>
</field>
<field name="discountFee">
<constraint annotation="javax.validation.constraints.DecimalMin">
<message>{com.alibaba.cb.api.order.model.ordermodel.discountFeeMin}</message>
<element name="value">0</element>
</constraint>
<constraint annotation="javax.validation.constraints.Digits">
<message>{com.alibaba.cb.api.order.model.ordermodel.discountFeeDigits}</message>
<element name="integer">6</element>
<element name="fraction">2</element>
</constraint>
</field>
<field name="additionalFee">
<constraint annotation="javax.validation.constraints.DecimalMin">
<message>{com.alibaba.cb.api.order.model.ordermodel.additionalFeeMin}</message>
<element name="value">0</element>
</constraint>
<constraint annotation="javax.validation.constraints.Digits">
<message>{com.alibaba.cb.api.order.model.ordermodel.additionalFeeDigits}</message>
<element name="integer">6</element>
<element name="fraction">2</element>
</constraint>
</field>
<field name="payway">
<constraint annotation="javax.validation.constraints.NotNull">
<message>{com.alibaba.cb.api.order.model.ordermodel.payway}</message>
</constraint>
</field>
<field name="payTimeout">
<constraint annotation="javax.validation.constraints.NotNull">
<message>{com.alibaba.cb.api.order.model.ordermodel.payTimeoutNotNull}</message>
</constraint>
//支持javascript表达式
<constraint annotation="org.hibernate.validator.constraints.ScriptAssert">
<message>{com.alibaba.cb.api.order.model.ordermodel.payTimeoutScript}</message>
<element name="lang">javascript</element>
<element name="script">
_this > new Date().getTime()
</element>
</constraint>
</field>
<field name="autoConfirmReceipt">
<constraint annotation="javax.validation.constraints.Min">
<message>{com.alibaba.cb.api.order.model.ordermodel.autoConfirmReceiptMin}</message>
<element name="value">5</element>
</constraint>
</field>
</bean>
</constraint-mappings>
从以上来看,xml其实就是annotation的另外一个翻版,但是意义却比较大,可以实现定义和校验实现分离
实施:
Configuration<?> config = Validation.byDefaultProvider().configure();
//对于xml文件
config.addMapping(new FileInputStream(validationFile));
基于annotation的校验
暂无