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的更多相关文章
随机推荐
- BestCoder Round #81 (div.2)C String
总体思路好想,就是在找K个不同字母的时候,卡时间. 看了大神代码,发现goto的!!!!998ms #include<cstdio> #include<cstring> #in ...
- javascript设计模式1
普通写法 function startAnimation(){ ... } function stopAnimation(){ ... } 对象类 /*Anim class*/ var Anim=fu ...
- Android webView 中loadData方法加载 带中文时出现乱码
WebView出现乱码用LoadData方法来解析html的,但是据说这是官方的一个BUG,不能用来解析中文. 采用loadDataWithBaseURL的方法,其中codeingType设置为utf ...
- POJ2752 - Seek the Name, Seek the Fame(KMP)
题目大意 给定一个字符串S,求出所有既是S的前缀又是S的后缀的子串长度 题解 从末尾位置倒推,经过的失配函数值就是题目要求求的 代码: #include <iostream> #inclu ...
- win7下wubi安装Ubuntu,重装win7后找回Ubuntu启动项
怀念一下我的win7,使用了将近5年,最近终于慢慢处于崩溃且无法修复的状态. 还是重新安装了. 原本是win7下使用wubi安装Ubuntu.重装win7后,肯定没有了Ubuntu的启动项. 具体恢复 ...
- centos6.4 yum kvm
1. 安装软件包: yum -y install virt-manager; yum install qemu-kvm yum install libvirt yum install libvi ...
- Spark shell里的语句探索
获得垃圾链接数据集的命令如下: wget http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/spam.data scala> v ...
- URL参数带中文,后台接收乱码解决方案
1.前台中文参数用encodeURIComponent()进行编码,如: var textName= encodeURIComponent(name); 2.对整个URL用encodeURI()进行编 ...
- PC-如何提高计算机的启动和关机的速度?
如何提高计算机的启动和关机的速度? 一.bios的优化设置 在bios设置的首页我们进入"advanced bios features"选项,将光标移到"frist bo ...
- Linux文件误删除恢复操作
作为一个多用户.多任务的操作系统,Linux下的文件一旦被删除,是难以恢复的.尽管删除命令只是在文件节点中作删除标记,并不真正清除文件内容,但是 其他用户和一些有写盘动作的进程会很快覆盖这些数据.不过 ...