数据验证框架 Apache BVal 简介(更新)

简介: Apache BVal (源码)是实体数据验证 Java Bean Validation 的参考实现。Apache BVal 提供了 JSR 303 规范中所有内置 constraint 的实现,用于对 Bean 中的字段的值进行约束定义、描述和验证。

Apache BVal源码)是实体数据验证 Java Bean Validation 的参考实现。Apache BVal 提供了 JSR 303 规范中所有内置 constraint 的实现,用于对 Bean 中的字段的值进行约束定义、描述和验证。若单单说 JSR 规范大渣可能还不清楚,但做过 POJO 的 Hibernate Validator 注解的朋友就知道是啥,——那为什么不使用主流的 Hibernate Validator 呢?因为这货净是个压缩包都已经 13mb 了(尽管可以有文档、源码其他在内),BVal 才只有 400 多 kb,而我只需要服务端验证而已,——天真的孩纸伤不起啊。俺的 ORM 也是 Mybatis 的,务求尽可能地轻量级。

Spring MVC 3.x 虽然自带了验证器 Validatior,可以在控制器中对表单提交的数据进行验证,但这个验证器是极其弱,因为你需要完全手工编码 if (null) else warn("不能空数据"),——太变态了(入下面的例子)——我们需要框架来减轻费时耗力的劳动,于是这类验证框架出现了。

    import org.springframework.validation.Errors;  
    import org.springframework.validation.ValidationUtils;  
    import org.springframework.validation.Validator;  
       
    public class UserValidator implements Validator {  
       
        public boolean supports(Class<?> clazz) {  
           // TODO Auto-generated method stub  
           return User.class.equals(clazz);  
        }  
       
        public void validate(Object obj, Errors errors) {  
           // TODO Auto-generated method stub  
           ValidationUtils.rejectIfEmpty(errors, "username", null, "Username is empty.");  
           User user = (User) obj;  
           if (null == user.getPassword() || "".equals(user.getPassword()))  
               errors.rejectValue("password", null, "Password is empty.");  
        }  
       
    }  

而我们理想的是这样的,在 POJO 身上声明验证条件的注解(Fields or Method 均可):

import javax.validation.constraints.Min;  
import javax.validation.constraints.NotNull;  
   
public class User {  
    @NotNull(message="用户名不能为空")  
private String username; private String password; private int age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @NotNull(message="密码不能为null") public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Min(value=10, message="年龄的最小值为10") public int getAge() { return age; } public void setAge(int age) { this.age = age; } }

好了,现在准备一下 BVal jar 包:

加入测试用例:

import static org.junit.Assert.assertNotNull;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.apache.bval.jsr.ApacheValidationProvider;
import org.junit.Test;

import com.ajaxjs.business.model.News;

public class TestValidation {
	@Test
	public void testValid() {
		News news = new News();
		ValidatorFactory avf = Validation.byProvider(ApacheValidationProvider.class).configure().buildValidatorFactory();
		Validator validator = avf.getValidator();
		//news.setName("dsdsa");
		Set<ConstraintViolation<News>> constraintViolations = validator.validate(news);

		for (ConstraintViolation<News> constraintViolation : constraintViolations) {
			System.out.println(constraintViolation.getMessage());
		}
		assertNotNull(constraintViolations);
	}
}

注解说明

@Vaild 是用于嵌套另外一个 pojo 的,如:

@NotNull
@Valid
private Person driver;
那么,

Car car = new Car(null, "", 1);
Person driver = new Person();
car.setDriver(driver);

Annotation Supported data types Use Hibernate metadata impact
@AssertFalse Boolean, boolean Checks that the annotated element is false None
@AssertTrue Boolean, boolean Checks that the annotated element is true None
@DecimalMax(value=, inclusive=) BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types; Additionally supported by HV: any sub-type of Number Checks whether the annotated value is less than the specified maximum, when inclusive=false. Otherwise whether the value is less than or equal to the specified maximum. The parameter value is the string representation of the max value according to the BigDecimal string representation. None
@DecimalMin(value=, inclusive=) BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types; Additionally supported by HV: any sub-type of Number Checks whether the annotated value is larger than the specified minimum, when inclusive=false. Otherwise whether the value is larger than or equal to the specified minimum. The parameter value is the string representation of the min value according to the BigDecimal string representation. None
@Digits(integer=, fraction=) BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types; Additionally supported by HV: any sub-type of Number Checks whether the annoted value is a number having up to integer digits and fraction fractional digits Defines column precision and scale
@Future java.util.Date, supported by HV, if the Joda Time date/time API is on the class path: any implementations of ReadablePartial and ReadableInstant Checks whether the annotated date is in the future None
@Max(value=) BigDecimal, BigInteger, byte, short, int, long and the respective wrappers of the primitive types; Additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number Checks whether the annotated value is less than or equal to the specified maximum Adds a check constraint on the column
@Min(value=) BigDecimal, BigInteger, byte, short, int, long and the respective wrappers of the primitive types; Additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the char sequence is evaluated), any sub-type of Number Checks whether the annotated value is higher than or equal to the specified minimum Adds a check constraint on the column
@NotNull Any type Checks that the annotated value is not null. Column(s) are not nullable
@Null Any type Checks that the annotated value is null None
@Past java.util.Date, java.util.Calendar; Additionally date/time API is on the class path: any implementations of ReadablePartial and ReadableInstant Checks whether the annotated date is in the past None
@Pattern(regex=, flag=) CharSequence Checks if the annotated string matches the regular expression regex considering the given flag match None
@Size(min=, max=) CharSequence, Collection, Map and arrays Checks if the annotated element's size is between min and max (inclusive) Column length will be set to max
@Valid Any non-primitive type Performs validation recursively on the associated object. If the object is a collection or an array, the elements are validated recursively. If the object is a map, the value elements are validated recursively.

翻译是:

Constraint 详细信息
@Null 被注释的元素必须为 null
@NotNull 被注释的元素必须不为 null
@AssertTrue 被注释的元素必须为 true
@AssertFalse 被注释的元素必须为 false
@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@DecimalMin(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max, min) 被注释的元素的大小必须在指定的范围内
@Digits (integer, fraction) 被注释的元素必须是一个数字,其值必须在可接受的范围内
@Past 被注释的元素必须是一个过去的日期
@Future 被注释的元素必须是一个将来的日期
@Pattern(value) 被注释的元素必须符合指定的正则表达式

Apache BVal 提供额外的注解,在 org.apache.bval.constraints.* 下。

注入到 Spring

如果用 Apache BVal 结合 Spring 是怎么做的呢?首先在 MVC 的 xml 配置文件中加入以下:

<mvc:annotation-driven validator="validator"/>

<!-- 数据验证 Validator bean -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="providerClass" value="org.apache.bval.jsr.ApacheValidationProvider" />
</bean>
<!-- // -->

这是一种全局的配置方式,我们注入 Bval 的验证器,另外,还要让 MVC 打开注解驱动。加上了 <mvn:annotation-driven/> 之后 Spring 会自动检测 classpath 下的 JSR-303 提供者并自动启用对 JSR-303 的支持。然后就可以在 POJO 中添加注解,而且要在控制器中声明 bean 的验证,如下例的 @Valid T news,否则 Bval 不会起作用。

/**
 * 新建 
 * @return
 */
@RequestMapping(method = RequestMethod.POST)
public String create(@Valid T news,  BindingResult result,Model model) {
	System.out.println("新建");
	if (result.hasErrors()) {
		LOGGER.info("create error!");
	}else{
		LOGGER.info("create ok!");
	}
	news.setService(getService());
	try {
		getService().create(news);
		model.addAttribute("newlyId", news.getId());
	} catch (ServiceException e) {
		model.addAttribute("errMsg", e.toString());
	}
	
	return "common/entity/json_cud";
}

一定要注意的是,控制器方法的参数顺序。Binding Result 必须在 Bean 后面。这是 Spring MVC 的约定。MVC 对控制器其他参数的顺序没什么规定,唯独这个 BindingResult 作了如此规定。目的是为了可以允许有多个 bean,于是也就有多个 BindingResult。

怎么处理错误就不详细说了,不同场景下要求不同。

自定义验证

可否自定义验证条件?我还没试,应该可以参考 Hibernate Validator 的做法。下面这篇文章说得很详细。

《Springmvc validator 验证的使用》

JSR 303 - Bean Validation 介绍及最佳实践

lk

调用 Apache BVal

07-18 补充如下:

可否脱离 Spring 运行?本身 BVal 就是一个独立的框架,因此答案不仅是可以的,而且非常简单。下面使用工厂和单例两种模式,返回一个校验器。

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.apache.bval.jsr.ApacheValidationProvider;

public enum MyValidatorFactory {
	SINGLE_INSTANCE {
		// BVal 与 JSR 接口结合,返回 ValidatorFactory 工厂
		ValidatorFactory avf = Validation.byProvider(ApacheValidationProvider.class).configure().buildValidatorFactory();

		@Override
		public Validator getValidator() {
			return avf.getValidator();
		}

	};
	
	/**
	 * 返回一个校验器
	 * @return 校验器
	 */
	public abstract Validator getValidator();
}

这里一点技巧就是通过 Java 枚举来实现单例。

好了,拿一个 Bean 来测试下,分别有通过和报错两种情况的测试。

import java.util.Set;

import static org.junit.Assert.*;
import org.junit.*;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import com.ajaxjs.framework.model.MyValidatorFactory;
import com.ajaxjs.framework.model.News;

public class TestValid {

	@Test
	public void testQuery()  {
		Validator v = MyValidatorFactory.SINGLE_INSTANCE.getValidator();
		News news = new News();
		news.setName("标题");
		Set<ConstraintViolation<News>> result = v.validate(news);
		assertTrue("应该通过校验", result.isEmpty());
		
		News news2 = new News();
		result = v.validate(news2);
		assertTrue("应该不通过校验", !result.isEmpty());
		
		System.out.println(result.size());
		for(ConstraintViolation<News> r : result) {
			System.out.println(r.getMessage());// 什么错?
			System.out.println(r.getPropertyPath());// 哪个字段错?
		}
		
	}
}

主要通过返回 ConstraintViolation 的 isEmpty() 方法来判断是否有错误。

目录
相关文章
|
22天前
|
存储 缓存 分布式计算
Apache Hudi数据跳过技术加速查询高达50倍
Apache Hudi数据跳过技术加速查询高达50倍
51 2
|
22天前
|
分布式计算 测试技术 Apache
如何不加锁地将数据并发写入Apache Hudi?
如何不加锁地将数据并发写入Apache Hudi?
38 0
|
22天前
|
关系型数据库 Apache 流计算
手把手教你实现 OceanBase 数据到阿里云数据库 SelectDB 内核版 Apache Doris 的便捷迁移|实用指南
本文介绍了如何将数据从 OceanBase 迁移到阿里云数据库 SelectDB 内核版 Apache Doris。提供 3 种数据同步方法 1. 使用 DataX,下载 DataX 并编写配置文件,通过 OceanBaseReader 和 DorisWriter 进行数据迁移。 2. 利用 Apache Doris 的 Catalog功 能,将 OceanBase 表映射到 Doris 并插入数据。 3. 通过Flink CDC,设置 OceanBase 环境,配置 Flink 连接器,实现实时数据同步。
手把手教你实现 OceanBase 数据到阿里云数据库 SelectDB 内核版 Apache Doris 的便捷迁移|实用指南
|
22天前
|
消息中间件 Kafka Apache
Apache Flink 是一个开源的分布式流处理框架
Apache Flink 是一个开源的分布式流处理框架
732 5
|
22天前
|
Java 应用服务中间件 Apache
简介Nginx,Tomcat和 Apache
简介Nginx,Tomcat和 Apache
简介Nginx,Tomcat和 Apache
|
14天前
|
消息中间件 Java Kafka
实时计算 Flink版操作报错之Apache Flink中的SplitFetcher线程在读取数据时遇到了未预期的情况,该怎么解决
在使用实时计算Flink版过程中,可能会遇到各种错误,了解这些错误的原因及解决方法对于高效排错至关重要。针对具体问题,查看Flink的日志是关键,它们通常会提供更详细的错误信息和堆栈跟踪,有助于定位问题。此外,Flink社区文档和官方论坛也是寻求帮助的好去处。以下是一些常见的操作报错及其可能的原因与解决策略。
|
8天前
|
OLAP 数据处理 Apache
众安保险 CDP 平台:借助阿里云数据库 SelectDB 版内核 Apache Doris 打破数据孤岛,人群圈选提速4倍
众安保险在CDP(Customer Data Platform,客户数据平台)建设中,通过引入阿里云数据库SelectDB版内核Apache Doris,成功打破了数据孤岛,并显著提升了人群圈选的速度
172 1
|
22天前
|
数据采集 机器学习/深度学习 Java
数据猎手:使用Java和Apache HttpComponents库下载Facebook图像
本文介绍了如何使用Java和Apache HttpComponents库从Facebook获取图像数据。通过设置爬虫代理IP以避免限制,利用HttpClient发送请求,解析HTML找到图像链接,然后下载并保存图片。提供的Java代码示例展示了实现过程,包括创建代理配置、线程池,以及下载图片的逻辑。注意,实际应用需根据Facebook页面结构进行调整。
数据猎手:使用Java和Apache HttpComponents库下载Facebook图像
|
22天前
|
分布式计算 Java Go
Golang深入浅出之-Go语言中的分布式计算框架Apache Beam
【5月更文挑战第6天】Apache Beam是一个统一的编程模型,适用于批处理和流处理,主要支持Java和Python,但也提供实验性的Go SDK。Go SDK的基本概念包括`PTransform`、`PCollection`和`Pipeline`。在使用中,需注意类型转换、窗口和触发器配置、资源管理和错误处理。尽管Go SDK文档有限,生态系统尚不成熟,且性能可能不高,但它仍为分布式计算提供了可移植的解决方案。通过理解和掌握Beam模型,开发者能编写高效的数据处理程序。
145 1
|
22天前
|
Apache 开发者
揭秘!Apache Hudi社区发展数据盘点
揭秘!Apache Hudi社区发展数据盘点
41 0

推荐镜像

更多