java开源验证框架OVAL帮助文档
OVAL验证框架帮助文档
目录
1. 注解说明
1.1、 @Assert
1.2、 @AssertFalse、@AssertTrue,@AssertNull
1.3、 @AssertURL
1.4、 @CheckWith
1.5、 @DateRange、@Future、@Past
1.6、 @Email
1.7、 @EqualToField,@Not EqualToField
1.8、 @HasSubstring
1.9、 @Length,@MaxLength,@MinLength
1.10、 @Size、@MinSize、@MaxSize
1.11、 @MemberOf、@NotMemberOf
1.12、 @NotBlank、@NotNull、@NotEmpty
1.13、 @NotNegative
1.14、 @Digits、@Range、@Max、@Min
1.15、 @NotMatchPatternCheck,@MatchPatternCheck
1.16、 @ValidateWithMethod
2. 使用总结
2.1. 字符类型
2.2. 数值类型
2.3. 布尔类型
2.4. 集合数组
2.5. 表达式或自定义
3. 自定义注解
3.1 定义注解
3.2 定义实现
4. maven依赖
java开源验证框架oval,功能非常强大,使用简单;
1. 注解说明
1.1、 @Assert
检查指定语言的表达式返回值是否为true;这里表达式是groovy。
参数 |
说明 |
expr |
表达式 |
lang |
指明脚本语言 |
errorCode |
错误编码(共有属性) (可以修改成自己的异常编码串) net.sf.oval.constraint.Assert(默认值) |
message |
错误描述(共有属性) |
when |
前置条件(共有属性) |
示例:下面验证登录名必须是用户名或用户编码; when表示前置条件;
@Assert(expr="_value==_this.userName || _value ==_this.userCode" ,lang="groovy",message="login name error." , ,when="groovy:_this.status>0")private String loginName;private int status;
1.2、 @AssertFalse、@AssertTrue,@AssertNull
@AssertFalse检查值是否为假、@AssertTrue检查值是否为真,主要参数when、errorCode、message.
Note: 该验证当值为null时也满足;当然我们可以结合@NotNull来使用;
@AssertNull说明:检查值是否为空, 与@NotNull相反;
1.3、 @AssertURL
参数 |
说明 |
connect(boolean) |
Specifies if a connection to the URL should be attempted to verify its validity. 是否发起连接进行尝试; |
检查值是否为有效的URL
Note: Thisconstraint is also satisfied when the value to validate is null, therefore youmight also need to specified @NotNull
1.4、 @CheckWith
参数 |
说明 |
Class<? extendsCheckWithCheck.SimpleCheck> |
value Check class to use for validation. 指明验证类 |
ignoreIfNull |
this constraint will be ignored if the value to check is null |
说明:
Check the value by a method of the sameclass that takes the value as argument and returns true if valid and false ifinvalid.
使用net.sf.oval.constraint.CheckWithCheck.SimpleCheck实现该接口的类中isSatisfied方法来判断,返回true有效,false无效;如果实现类是内部的,必须为静态类。
示例:验证User类中的age字段
@CheckWith(value=CheckAge.class,message="agemust in (18~65)")private int age;
验证类如下:
publicclass CheckAge implements CheckWithCheck.SimpleCheck {private static final long serialVersionUID =1L;@Overridepublic boolean isSatisfied(ObjectvalidatedObject, Object value) {User user = (User)validatedObject;int age = user.getAge();if(age <18 || age > 65)return false;elsereturn true;}}
1.5、 @DateRange、@Future、@Past
参数 |
说明 |
min |
/** * The upper date compared against in the format specified with the dateFormat parameter. * If not specified then no upper boundary check is performed.<br> * Special values are: * <ul> * <li><code>now</code> * <li><code>today</code> * <li><code>yesterday</code> * <li><code>tomorrow</code> * </ul> */ |
max |
/** * The lower date compared against in the format specified with the dateFormat parameter. * If not specified then no lower boundary check is performed.<br> * Special values are: * <ul> * <li><code>now</code> * <li><code>today</code> * <li><code>yesterday</code> * <li><code>tomorrow</code> * </ul> */ |
Check if the date is within the a daterange.
Note: This constraint is also satisfied when the value to validate isnull, therefore you might also need to specified @NotNull
示例:字符串类型一样可以;
@Future(message="date isfuture.") 不能验证字符串,改用自定义类型@CFuture@Past(message="date is past.") 不能验证字符串,改用自定义类型@CPast@DateRange可以验证字符串类型,请查看源码验证@DateRange(min="2010-10-01",max="now",message="dateis error.")private String birthday;
1.6、 @Email
Check if the value is a valid e-mailaddress. The check is performed based on a regular expression.
Note: Thisconstraint is also satisfied when the value to validate is null, therefore youmight also need to specified @NotNull
示例:
@Email(message="email is error.")private String email;
1.7、 @EqualToField,@Not EqualToField
参数 |
说明 |
boolean useGetter |
useGetter default false; |
Check if value equals the value of thereferenced field.
示例:检查userCode是否和userName相等;使用get方法。
@EqualToField(value="userName",message="mustequals userName",useGetter=true)private String userCode;
@NotNull(message="not null")@NotEqualToField(value="userCode",message="can'tequals userCode")private String userName;
1.8、 @HasSubstring
参数 |
说明 |
value |
需要给的子串 |
ignoreCase(boolean) |
ignoreCase default false; |
Check if the string contains a certainsubstring.
Note: Thisconstraint is also satisfied when the value to validate is null, therefore youmight also need to specified @NotNull
示例:
@HasSubstring(value="san",ignoreCase=true,message="mustcontains 'san'")privateString userCode;
1.9、 @Length,@MaxLength,@MinLength
参数 |
说明 |
max |
最大长度,默认为:Integer.MAX_VALUE |
min |
默认值为0 |
@MaxLength和 @MinLength只有value属性;表示和value进行比较;
Checkif the string representation has certain length. 检查字符串的长度
@Length(max=10,message="最大长度不能超过10")private String code;
1.10、 @Size、@MinSize、@MaxSize
参数 |
说明 |
max |
|
min |
@MaxSize 和 @MinSize只有value属性;表示和value进行比较,判断array,map, 或者 collection大小;
min和max是@size的属性;
Check if the array,map, or collection has the given size. For objects of other types thelength of their String representation will be checked. 检查array、map或集合的大小;其他类型的对象检查对应字符串的长度;建议字符长度使用@Length验证。
Note: Thisconstraint is also satisfied when the value to validate is null, therefore youmight also need to specified @NotNull
1.11、 @MemberOf、@NotMemberOf
参数 |
说明 |
value 字符串数组 |
|
ignoreCase |
默认值false |
Check if the string representation iscontained in the given string array.
检查值是否包含在给定的数组中;@NotMemberOf实现相反效果;
1.12、 @NotBlank、@NotNull、@NotEmpty
参数 |
说明 |
NotBlank |
Check if the string representation is not empty and does not only contain white spaces. |
NotNull |
Check if not null. |
NotEmpty |
Check if the string representation is not empty (""). |
1.13、 @NotNegative
Check if the number is greater or equalzero. 检查值是否为非负数
Note: Thisconstraint is also satisfied when the value to validate is null, therefore youmight also need to specified @NotNull
1.14、 @Digits、@Range、@Max、@Min
@Range 有max和min 属性,检查数值类型的范围;
Check if the number is in the given range.(和Double进行比较)
@Min
Check if the number is greater than orequal to X.
@Max
Check if the number is smaller than orequal to X.
@Digits
Check if the String representation has thegiven max/min number of integral and fractional digits.
检查字符串形式的数字范围,对应属性如下
maxFraction = Integer.MAX_VALUE;maxInteger = Integer.MAX_VALUE;minFraction = 0;minInteger = 0;
示例:
1.15、 @NotMatchPatternCheck,@MatchPatternCheck
Check if the specified regular expressionpattern is or not matched. 正则表达式验证
参数 |
说明 |
pattern |
The regular expression(s) that must match or not match |
1.16、 @ValidateWithMethod
Check the value by a method of the sameclass that takes the value as argument and returns true if valid and false ifinvalid.
验证值作为参数,使用同一个类的某个方法返回值的布尔值进行验证;
方法必须是和验证值在同一类中。
参数 |
说明 |
methodName String |
name a the single parameter method to use for validation |
Class<?> parameterType |
type of the method parameter 方法的参数,及被验证值的类型 |
2. 使用总结
总结说明针对不同场景的主要使用那些注解。
2.1. 字符类型
@AsserURL、@Email、@Length、@MaxLength、@MinLength
@NotNull、@NotBlank、@NotEmpty、
@Digits、@HasSubstring
2.2. 数值类型
@Range、@Max、@Min、@NotNegative
2.3. 布尔类型
@AssertFalse、@AssertTrue
2.4. 集合数组
@Size、@MaxSize、@MinSize、@MemberOf、@NotMemberOf
2.5. 表达式或自定义
@Assert、@CheckWith、@NotMatchPatternCheck,@MatchPatternCheck、
@ValidateWithMethod
3. 自定义注解
@Past和@Future不能验证字符串类型的日期;自定义@CPast和@CFuture,都提供要给参数指定日期格式,默认为:yyyy-MM-dd;
3.1 定义注解
@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.METHOD})@Constraint(checkWith = CPastCheck.class)public @interface CPast {Stringmessage() default "日期必须小于现在.";StringdateFormat() default "yyyy-MM-dd";}
3.2 定义实现
public class CPastCheck extends AbstractAnnotationCheck<CPast> {private static final long serialVersionUID = 1L;private String dateFormat;public void configure(final CPast constraintAnnotation) {super.configure(constraintAnnotation);setDateFormat(constraintAnnotation.dateFormat());}public boolean isSatisfied(Object validatedObject, Object valueToValidate,OValContext context, Validator validator) throws OValException {SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);if(valueToValidate instanceof String) {try {Datedate = sdf.parse((String) valueToValidate);return date.before(new Date());}catch (ParseException e) {e.printStackTrace();super.setMessage("日期格式错误,无法验证,请修改成正确格式.");return false;}}return false;}public String getDateFormat() {return dateFormat;}public void setDateFormat(String dateFormat) {this.dateFormat= dateFormat;}}
4. maven依赖
<dependency><groupId>net.sf.oval</groupId><artifactId>oval</artifactId><version>1.81</version>
</dependency>
java开源验证框架OVAL帮助文档相关推荐
- java开源验证框架OVAL
@Length,@MaxLength,@MinLength maxLength,minLength只有value属性,表示和value进行比较 min和max是Length的属性 汉字算一个长度 @N ...
- java oval 使用_java开源验证框架OVAL应用实例
java开源验证框架OVAL应用实例 对oval验证框架进行分类,并针对常用的验证规则进行总结,从而可以抽象成模型可以定义,达到定义后自动生成,减少开发人员的工作量,同时也规范化代码结构. 1. 验 ...
- 开源语义理解框架 Clause API 文档:快速实现聊天机器人
在上一篇文章<基于开源语义理解框架 Clause 实现聊天机器人 > 中,很多读者关心如何使用 Clause,在 Clause Wiki 文档中心 中也有很多指导使用的资料,现将 API ...
- Java开源Web框架
WebWork 点击次数:18070 WebWork 是由OpenSymphony组织开发的,致力于组件化和代码重用的拉出式MVC模式J2EE Web框架.WebWork目前最新版本是2.1,现 ...
- Java开源JEE框架
Spring Framework [Java开源JEE框架] Spring是一个解决了许多在J2EE开发中常见的问题的强大框架. Spring提供了管理业务对象的一致方法并且鼓励了注入对接口编程而不 ...
- Java开源 J2EE框架(二)
Java开源 J2EE框架(二) 2007-01-06 12:34 Jofti [Java开源 其它开源项目] Jofti可对在缓存层中(支持EHCache,JBossCache和OSCache)的对 ...
- java开源服务框架_Java框架服务
Java从诞生到现在,一路飙升,可以说红遍全球,红到发紫.随着Java的流行,促生了许多java框架:Spring.WebWork.Struts.HIbernate.JDiy.JFinal.Quart ...
- 从转载阿里开源项目 Egg.js 技术文档引发的“版权纠纷”,看宽松的 MIT 许可该如何用?
作者 | 苏宓.彭慧中 出品 | CSDN(ID:CSDNnews) 开源迅速发展的这两年,很多内部问题逐渐凸显出来,如安全.版权.协议使用等. 近日,来自V2EX社区中一位开发者 @an168ban ...
- Java 使用word模板创建word文档报告教程
上面是java 利用word模板生成的一个word报告文档,利用的是第三方类库Poi-tl 是实现的. poi-tl是一个基于Apache POI的Word模板引擎,也是一个免费开源的Java类库,你 ...
最新文章
- python类的成员函数_python特殊成员函数
- mysql5.6 icp mrr bak_【mysql】关于ICP、MRR、BKA等特性
- 第36-37 Tomcat SVN
- java大文件解析_java大文件(百M以上)的上传下载实例解析
- oracle数据库日期格式的运算,Oracle时间类型date,timestamp时间差计算
- dns在企业网络中的应用-1
- java response设置403,java.io.IOException:服务器返回URL的HTTP响应代码:403
- Filecoin网络目前总质押量约为3314万枚FIL
- mysql inno_mysql inno优化配置方法
- 测试管理工具实践(小组作业)
- 机器视觉算法与应用-双语版-学习笔记
- 高交会|华创芯光邀您一起畅游可见光通信的世界
- Daemontools和Supervisor管理linux常驻进程
- 2020中国边缘计算企业20强
- 阿里员工爆出最好用的python库推荐!!--random随机数生成【原文附代码】
- xamp配置虚拟域名_如何下载,安装和配置XAMP以创建网页?
- MCV模型流程图整理
- 不同风格吉他曲目收录
- JZOJ5424. 【NOIP2017提高A组集训10.25】凤凰院凶真
- 请问深圳的准生证在哪办理?
热门文章
- Convert hex to base64
- canvas动画:自由落体运动
- matlab 音频fft,在wav文件和FFT的matlab中的Audioread
- java.net cidr接口_【算法】CIDR集合的算法
- Mysql之视图的创建、修改、查看、删除
- 创建一个有向图,并输出各个节点的出度和入度(邻接矩阵存储)
- 【新能源】从“材料”到“电池组”一文读懂动力电池生产全流程!
- 用Python实现四叉树(quad tree)
- 她让我把电脑带回家。 -您是如何开始计算机和编程的?
- 日本寿司第一人有多夸张?米饭用扇子降温,学徒十年才有资格煎蛋