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的更多相关文章
随机推荐
- NOIP2015 子串 (DP+优化)
子串 (substring.cpp/c/pas) [问题描述] 有两个仅包含小写英文字母的字符串 A 和 B.现在要从字符串 A 中取出 k 个 互不重 叠 的非空子串,然后把这 k 个子串按照其在字 ...
- 判断是否已安装.net framework
1.检测 %SystemRoot%\System 目录下的MSCorEE.dll文件,如果存在,则表明.net framework 已安装. 2.检测一下注册表项的子项: KEY_LOCAL_MACH ...
- POJ2923--Relocation(01背包+状压dp)
果然对状压DP,我根本就不懂=.= /************************************************** Problem: 2923 User: G_lory Mem ...
- 问题-[Delphi]PixelFormat 图像颜色的数据格式
PixelFormat: (指定图像中每个像素的颜色数据的格式) Delphi 微软 ...
- 【三支火把】---队列和栈的C程序实现
这几天总结了C语言的队列,栈的实现方法,在此总结一下:一.栈 首先从栈开始,诚然,相信学习过数据结构的你,肯定应该知道栈是什么东西了,如果不知道也没事每一句话我就可以帮你总结--数据只在栈顶进行插入和 ...
- Redis_基本类型介绍和指令___2
1.hash Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象. Redis 中每个 hash 可以存储 232 - 1 键值对(40多亿). ...
- CAS认证(3):验证用户信息
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- CAS 之 集成RESTful API
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 前端JS开发框架-DHTMLX--dhtmlXTree
介绍 dhtmlxTree是一个功能丰富的JavaScript树菜单 它允许您快速添加一个好看的,基于ajax的web页面的分层树. treeview支持在线节点编辑.先进的拖放,三态复选框等等.由 ...
- google DNS
DNS 8.8.8.8 然后 C:\Windows\System32\drivers\etc\hosts 108.111.1.1 www.s.com