前言:

网上一些朋友分享了关于hibernate-validator的使用方法,但是不是缺少关联库信息,就是提供的参考代码中缺少自定类。

希望我这一篇博客能够让你顺利的跑出预期的结果。

如果有错,可以给我留言。

英文好的朋友可以参考官网的getting started。

http://hibernate.org/validator/documentation/getting-started/

一、环境

hibernate-validator库必须运行的JDK版本为1.6及以上。

二、hibernate-validator库及依赖

 classmate-1.3..jar
hibernate-validator-5.3..Final.jar
javax.el-2.2..jar
javax.el-api-2.2..jar
jboss-logging-3.3..Final.jar
validation-api-1.1..Final.jar

三、假设我们构造了一个Car类

 public class Car {

     @NotNull
private String manufacturer; @NotNull
@Size(min = , max = )
private String licensePlate; @Min()
private int seatCount; public Car(String manufacturer, String licencePlate, int seatCount) {
this.manufacturer = manufacturer;
this.licensePlate = licencePlate;
this.seatCount = seatCount;
} public String getManufacturer() {
return manufacturer;
} public void setManufacturer(String manufacturer) {
this.manufacturer = manufacturer;
} public String getLicensePlate() {
return licensePlate;
} public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
} public int getSeatCount() {
return seatCount;
} public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
} }

四、如何校验呢?我们看看这个测试类

 public class CarTest {

     public static void main(String[] args) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Car car = new Car(null, "苏A999999", ); Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car);
for (ConstraintViolation<Car> constraintViolation : constraintViolations)
System.out.println("错误:" + constraintViolation.getMessage());
}
}

五、结果如何?

 十一月 ,  :: 下午 org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.3..Final
错误:不能为null
错误:最小不能小于2

六、真是简单易用!虽然这个库重复“发明”了轮子。

七、你注意到了吗?结果是自动国际化了的!

非WEB项目中引入Hibernate Validator的更多相关文章

  1. spring 项目中使用 hibernate validator验证输入参数

    1 hibernate validator 官方文档:https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_si ...

  2. 非web项目中使用XDT的实例(续)

    简化 上个实例中,我们是引用了target文件来遍历项目中的xdt项. 当项目比较小,我们又只需要针对固定的config文件进行更替时,可以使用更简化的方法 不进行项目配置文件中的插入<Impo ...

  3. java web项目中引入spring

    自己动手实践了一次,发生中间出了一下问题,现整理出来,供参考. Step1: 新建一个java web项目 Step2:下载spring的jar包http://repo.spring.io/libs- ...

  4. web项目中引入jquery easyui

    jQuery easyui是一个基于jquery的用户界面插件集合,可以做出各种炫酷页面效果,大中型项目都可以使用些框架,非常好用,而且它有中文网,提供了大量的demo.下面我们看怎么将它引入到项目中 ...

  5. Log4j2在WEB项目中配置

    最近决定在新WEB项目中使用新的日志系统Log4j2. 官方介绍和学习文档网址为http://logging.apache.org/log4j/2.x/ 首先在WEB项目中引入以下几个jar包: ① ...

  6. 转载 Log4j2在WEB项目中配置

    最近决定在新WEB项目中使用新的日志系统Log4j2. 官方介绍和学习文档网址为http://logging.apache.org/log4j/2.x/ 首先在WEB项目中引入以下几个jar包: ① ...

  7. 转 web项目中的web.xml元素解析

    转 web项目中的web.xml元素解析 发表于1年前(2014-11-26 15:45)   阅读(497) | 评论(0) 16人收藏此文章, 我要收藏 赞0 上海源创会5月15日与你相约[玫瑰里 ...

  8. Java web项目中java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    原来是tomcat找不到MYSQL JAR包的问题.后来又把mysql-connector-java-5.1.7-bin.jar导入到tomcat的lib目录下面就ok了,嘿…… 在java项目中,只 ...

  9. 【.net深呼吸】非 Web 项目使用缓存

    从.net 4 开始,非web项目也可以使用缓存技术,故曰:.net 4 乃框架成熟之标志也. 对于缓存嘛,耍过 ASP.NET 的伙伴们肯定知道,这么说吧,就是将一些使用频率较高的数据放于内存中,并 ...

随机推荐

  1. c++之map

    题目描述:     哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮 ...

  2. IOS第18天(5,CABasicAnimation基本动画)

    ******* #import "HMViewController.h" @interface HMViewController () @property (nonatomic, ...

  3. 精通JavaScript的this关键字

    小提示 阅读本文,您需要了解JS的基本常识.您将花费40分钟完成本文的阅读. JS中的this关键字让很多新老JS开发人员都感到困惑.这篇文章将对this关键字进行完整地阐述.读完本文以后,您的困惑将 ...

  4. 给RecyclerView实现的GridView加上HeaderView和FooterView

    给RecyclerView设置布局管理器 GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3); 写适配器,添加子项 ...

  5. css公共样式

    /* ==================================================================== @ set browser style ======== ...

  6. 求解PDE的多重网格法(MG)

    多重网格法相对于普通的Jacobi迭代或者G-S迭代等能够得到和未知数的个数成线性的高效运行时间的主要原因在于:迭代初值的一步步接近真值和G_S方法的前面几步的快速收敛性. 先看一张图[1]: 这张图 ...

  7. 采用CSS3设计的登录界面,动态效果(动画)

    与上一篇的“采用CSS3设计的登陆界面”的相同,只是样式style添加了CSS3的动画元素. style内容如下: <style> html,body,div{ margin:0; pad ...

  8. ansible 安装使用

    ansible ansible源码安装 yum -y install python-jinja2 PyPAML python-parmiko python-babel python-crypto ta ...

  9. php +html5 websocket 聊天室

    针对内容比较长出错,修改后的解码函数 和 加码函数 原文请看上一篇 http://yixun.yxsss.com/yw3104.html function uncode($str,$key){ $ma ...

  10. For,Function,Lazy

    package com.dtgroup.study import scala.io.Source object ForFunctionLazy { def main(args: Array[Strin ...