只要有接口,就会有参数的校验,目前开源的校验框架已经非常多了,不过不得不提一下Oval。
OVal 是一个可扩展的Java对象数据验证框架,验证的规则可以通过配置文件、Annotation、POJOs 进行设定。可以使用纯 Java 语言、JavaScript 、Groovy 、BeanShell 等进行规则的编写。
使用起来也非常简单
public class OvalTest { @Min(20) private int age; @Length(min = 6, max = 10) private String name; public static void main(String[] args) { OvalTest ovalTest = new OvalTest(); ovalTest.age = 10; ovalTest.name = "kolor"; Validator validator = new Validator(); List<ConstraintViolation> ret = validator.validate(ovalTest); System.out.println(ret); } }
<p><?xml version="1.0" encoding="UTF-8"?> <!-- <!DOCTYPE oval PUBLIC "-//OVal/OVal Configuration DTD 1.3//EN" "<a href="http://oval.sourceforge.net/oval-configuration-1.3.dtd">http://oval.sourceforge.net/oval-configuration-1.3.dtd</a>"> --> <oval xmlns="<a href="http://oval.sf.net/oval-configuration">http://oval.sf.net/oval-configuration</a>" xmlns:xsi="<a href="http://http://www.w3.org/2001/XMLSchema-instance">http://http://www.w3.org/2001/XMLSchema-instance</a>" xsi:schemaLocation="<a href="http://oval.sf.net/oval-configuration">http://oval.sf.net/oval-configuration</a> ../../../../../../../../main/resources/net/sf/oval/configuration/xml/oval-configuration.xsd" > <!-- define a constraint set --> <!--<constraintSet id="user.userid"> <notNull message="{context} is null" /> <matchPattern message="{context} does not match the pattern {pattern}"> <pattern pattern="<a href="file://\\d{15}|\\d{17}+[\\dX">\\d{15}|\\d{17}+[\\dX</a>]" flags="0" /> </matchPattern> </constraintSet>--></p><p> <class type="com.haove.oval.TrainData" overwrite="false" applyFieldConstraintsToSetters="false"> <field name="sfzh"> <matchPattern message="身份证号不符合规则"> <pattern pattern="[0-9]{15}|[0-9]{17}[0-9X]" flags="0" /> </matchPattern> </field> <field name="xm"> <length min="1" max="24" message="姓名为 {min} 至 {max}个字符" /> <validateWithMethod methodName="isValid" parameterType="java.lang.String" message="姓名与学籍库不一致"/> </field> <field name="qjStart"> <length min="1" message="区间起点不能少于{min}个字符" /> </field> <field name="qjEnd" overwrite="true"> <length min="1" message="区间终点不能少于{min}个字符" /> </field></p><p> <field name="rxrq"> <matchPattern message="入学日期为8位数字"> <pattern pattern="[0-9]{8}"/> </matchPattern> </field> <field name="yhcs"> <matchPattern message="优惠次数为一位或两位数字"> <pattern pattern="[0-9]{1,2}" flags="0" /> </matchPattern> </field> <field name="kxlh"> <length min="1" message="卡序列号不能少于{min}个字符" /> </field> </class> </oval></p>
这个xml里包含了<notNull />,<notBlank />,<validateWithMethod />,<dateRange />,<pattern />等几种校验,比较有特点的是可以自定义方法校验validateWithMethod,有了这个就可以将校验扩展的更加强大,比如说可以将数据跟数据库里的数据进行比较校验。还有其他校验,可以参考源码,或问我。
另外,经查源码,发现方法public List<ConstraintViolation> validateFieldValue(final Object validatedObject, final Field validatedField, final Object fieldValueToValidate)可以只校验自己想校验的field,不会校验全部field,这个在有些时候还是蛮有用的。
另外附上,使用xml校验的java源码例子:
import java.lang.reflect.Field; import java.util.List; import net.sf.oval.ConstraintViolation; import net.sf.oval.Validator; import net.sf.oval.configuration.xml.XMLConfigurer; public class TrainData { private String xm; private String sfzh; private String qjStart; private String qjEnd; private String rxrq; // @ValidateWithMethod(methodName = "isValid", parameterType = String.class) private String yhcs; private String kxlh; public TrainData(){} public TrainData(String xm, String sfzh, String qjStart, String qjEnd, String rxrq, String yhcs, String kxlh){ this.xm = xm; this.sfzh = sfzh; this.qjStart = qjStart; this.qjEnd = qjEnd; this.rxrq = rxrq; this.yhcs = yhcs; this.kxlh = kxlh; } private boolean isValid(String xm) { //to do 数据库根据身份证号查询出的姓名进行比较 if("1".equals(xm)) return true; return false; } public static void main(String[] args) { try{ TrainData data = new TrainData("刘艳梅","11010619770201304","","","20050101","2","123"); XMLConfigurer x2 = new XMLConfigurer(); x2.fromXML(XMLConfigurationTest.class.getResourceAsStream("TrainDataValidation.xml")); Validator validator = new Validator(x2); // Validator validator = new Validator(); // Field f=TrainData.class.getDeclaredField("xm"); // List<ConstraintViolation> violations = validator.validateFieldValue(new TrainData(), f, "12"); List<ConstraintViolation> violations = validator.validate(data); for(ConstraintViolation var:violations){ System.out.println(var.getMessage()); } }catch(Exception ex){ } } }
参考:http://blog.csdn.net/haove/article/details/6901366
Java对象校验框架之Oval
时间: 2024-10-15 04:22:54