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的更多相关文章

随机推荐

  1. 使用 IntelliJ IDEA 导入 Spark 最新源码及编译 Spark 源代码

    前言   其实啊,无论你是初学者还是具备了有一定spark编程经验,都需要对spark源码足够重视起来. 本人,肺腑之己见,想要成为大数据的大牛和顶尖专家,多结合源码和操练编程. 准备工作 1.sca ...

  2. 有关gcc的扩展__attribute__((unused))

    ================================ Author: taoyuetao Email: tao_yuetao@yahoo.com.cn Blog: taoyuetao.cu ...

  3. system partition table

    转载内容 摘录部分我的笔记的中doc,和大家一起感受Oracle 11g在分区方面的增强--System Partitioning 系统分区的特点 ●系统分区与其他分区相比,一个最根本的区别就是不需要 ...

  4. Centos环境下Tomcat启动缓慢

    最近项目上线部署的时候,发现一个问题.Tomcat在启动过程中耗费了很长的时间.查看日志,发现耗时最长的地方是:INFO [localhost-startStop-1] org.apache.cata ...

  5. 用lambda构建ORM查询语句

    本文介绍如何解析lambda表达式来获取一个满足条件的查询语句. 先看个截图  通过设置实体对象Article_Content的查询表达式,就可以获取对应的参数化SQL语句,使用起来很方便,减少了代码 ...

  6. POJ 2728 Desert King

    Description David the Great has just become the king of a desert country. To win the respect of his ...

  7. 【设计模式】单例设计模式的N中Java实现方法

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17359719 特点 单例模式的特点: 1.只能有一个实例: 2.必须自己创建自己的一个实例 ...

  8. iframe框架自适应高度 uncanght SecurityError: Blocked a frame with origin "null" from accessing a frame ....

    来源于crm项目的contact/edit.html 一.背景是这样的 最近在做crm系统的前端页面,有一个页面呢,点击“查看全部信息”时会弹出,这个弹窗里面又有分页导航,分页不是使用ajax 异步刷 ...

  9. onSubmit的使用

    在web开发中,我们经常会遇到,一点回车键表单就自己提交的问题,能不能禁用回车键呢,答案是肯定的. Html代码  <from action="" method=" ...

  10. scala编程笔记(三)类,字段和方法

    类,字段和方法 类是对象的蓝图,能够通过new来创建对象.在类的定义里能够有字段和方法.统称member val还是var都是指向对象的变量(var定义的字段可又一次赋值),def定义方法,包括可运行 ...