hibernate_validator_01
1.环境准备(Maven工程)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.wy</groupId>
<artifactId>validator_1</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.2.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency> </dependencies> </project>
2.准备po类
package po; import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size; /**
* Created by Administrator on 2016/7/12.
*/
public class Car {
@NotNull
private String manufacturer;
@NotNull
@Size(min = 2, max = 14)
private String licensePlate;
@Min(2)
private int seatCount;
public Car(String manufacturer, String licencePlate, int seatCount) {
this.manufacturer = manufacturer;
this.licensePlate = licencePlate;
this.seatCount = seatCount;
}
}
3.验证测试
package test; import org.junit.BeforeClass;
import org.junit.Test;
import po.Car; import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set; /**
* Created by Administrator on 2016/7/12.
*/
public class CarTest {
private static Validator validator;
/*获取验证器*/
@BeforeClass
public static void setUp() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();
}
@Test
public void test1() {
Car car = new Car(null, "DD-AB-123", 4);
//validate() 方法会返回一个set的 ConstraintViolation 的实例的集合, 我们可以通过遍历它来查看哪些验证错误
Set<ConstraintViolation<Car>> constraintViolations = validator.validate(car);
System.out.println(constraintViolations.size());
System.out.println(constraintViolations);
//[ConstraintViolationImpl{interpolatedMessage='不能为null', propertyPath=manufacturer, rootBeanClass=class po.Car, messageTemplate='{javax.validation.constraints.NotNull.message}'}]
}
@Test
public void licensePlateTooShort() {
Car car = new Car("Morris", "D", 4);
Set<ConstraintViolation<Car>> constraintViolations =
validator.validate(car);
System.out.println(constraintViolations.size());
System.out.println(constraintViolations);
//[ConstraintViolationImpl{interpolatedMessage='个数必须在2和14之间', propertyPath=licensePlate, rootBeanClass=class po.Car, messageTemplate='{javax.validation.constraints.Size.message}'}]
}
@Test
public void seatCountTooLow() {
Car car = new Car("Morris", "DD-AB-123", 1);
Set<ConstraintViolation<Car>> constraintViolations =
validator.validate(car);
System.out.println(constraintViolations.size());
System.out.println(constraintViolations);
//[ConstraintViolationImpl{interpolatedMessage='最小不能小于2', propertyPath=seatCount, rootBeanClass=class po.Car, messageTemplate='{javax.validation.constraints.Min.message}'}]
}
@Test
public void carIsValid() {
Car car = new Car("Morris", "DD-AB-123", 2);
Set<ConstraintViolation<Car>> constraintViolations =
validator.validate(car);
System.out.println(constraintViolations.size());
System.out.println(constraintViolations);
//此时为一切正常
}
}
hibernate_validator_01的更多相关文章
随机推荐
- c++学习_2
这里承接上一篇文章,继续记录关于继承的那些事儿... NVI(non-Virtual Interface)和strategy模式 NVI模式和strategy模式是两种不同的方法,可以用来替代virt ...
- Storm系列(九)架构分析之Supervisor-同步Nimbus的事件线程
Supervisor由三个线程组成,一个计时器线程和两个事件线程. 计时器线程负责维持心跳已经更新Zookeeper中的状态,还负责每隔一定的时间将事件线程需要执行的事件添加到其对应的队列中. 两个事 ...
- Windows 服务 创建 和 安装 -摘自网络
What a Windows Service is Enables you to create long-running executable applications that run in the ...
- 问题-[delphi2007、2010]无法二次启动,报EditorLineEnds.ttr被占用,进程一直有bds.exe?
问题现象:delphi2007.2010无法二次启动,报EditorLineEnds.ttr被占用,而且进程中一直有bds.exe的进程? 问题原因:问题处理:方法一:可能是系统更新的东东造在的.KB ...
- 问题-[VMware Workstation]断电后,重启电脑,之后就提示“内部错误”
问题现象:突然断电后,重启电脑,再打开VMware Workstation,启动不了.之后就提示“内部错误”.问题原因:希望高人指点.问题处理:关闭VMware Workstation,在快捷方式上, ...
- 问题-关于sharemem程序访问WEB出现内存错误处理
[delphi技术] 关于sharemem造成dll错误的处理办法问题现象:如果程序和dll之间用string作为参数传递时容易出现错误问题处理:需要在程序的uses中使用sharemem.这个sha ...
- JavaScript Garden2
Types Equality and Comparisons JavaScript has two different ways of comparing the values of objects ...
- 用gooreplacer来加速你的浏览器
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:用gooreplacer来加速你的浏览器.
- Android 定时任务高度【schedule】与【scheduleAtFixedRate】差别
在android中调度定时任务有两种方法 1.schedule 2.scheduleAtFixedRate 这两种方法的差别在于 首次调用时间(Date when)这个參数 <span styl ...
- ApkTool动态打包
引言: APK在推广的时候可能会须要动态打包APK.比方公布到不同渠道的时候,须要在manifest文件里改动渠道信息.或者app在推广的时候.须要在apk包里面加上推广人信息等. 环境变量: 1.J ...