【Bean校验】
关键概念澄清:bean validation是规范,通过JSR定义。 Hibernate validation是具体的实现。
参考:
https://beanvalidation.org/2.0/ 规范官网,介绍规范演进,JSR1.0->1.1->2.0 定义和实现方(Hibernate validation)
https://www.baeldung.com/javax-validation 快速上手样例
https://www.cnblogs.com/yourbatman/p/13595278.html
一、Bean Validation 规范演进:
JSR 303: V1.0
JSR 349: V1.1
JSR 380: V2.0 因2018年3月,Oracle 决定把 JavaEE 移交给开源组织Eclipse基金会(Jakarta EE),所以后续POM中由javax变更为jakarta
<dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.0.0.GA</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.1.Final</version> </dependency> <dependency> <groupId>jakarta.validation</groupId> <artifactId>jakarta.validation-api</artifactId> <version>2.0.1</version> </dependency>
https://beanvalidation.org/2.0/ 官网:
二、 Hibernate validation
参考上章节,作为Bean Validation规范的实现,实际使用中仅引用当前JAR即可。
快速上手参考:https://www.baeldung.com/javax-validation,以下内容摘抄关键部分附上。
POM:必须添加EL的仓库,否则会提示报错
<dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> <version>6.0.13.Final</version> </dependency> <!-- JSR 380 supports variable interpolation, allowing expressions inside the violation messages. To parse these expressions, we'll add the javax.el dependency from GlassFish, that contains an implementation of the Expression Language specification: --> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.el</artifactId> <version>3.0.0</version> </dependency>
bean定义
public class User { @NotNull(message = "Name cannot be null") private String name; @AssertTrue private boolean working; @Size(min = 10, max = 200, message = "About Me must be between 10 and 200 characters") private String aboutMe; @Min(value = 18, message = "Age should not be less than 18") @Max(value = 150, message = "Age should not be greater than 150") private int age; @Email(message = "Email should be valid") private String email; // standard setters and getters }
校验
User user = new User(); user.setWorking(true); user.setAboutMe("Its all about me!"); user.setAge(10); ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); Validator validator = validatorFactory.getValidator(); Set<ConstraintViolation<User>> violations = validator.validate(user); for (ConstraintViolation<User> violation : violations) { System.err.println(violation.getMessage()); }
结果:
Name cannot be null
Age should not be less than 18
开发过程:
1、Some frameworks — such as Spring — have simple ways to trigger the validation process by just using annotations. This is mainly so that we don't have to interact with the programmatic validation API. // Spring集成使用更便捷
Now let's go the manual route and set things up programmatically:
2、To validate a bean, we first need a Validator object, which is built using a ValidatorFactory.
3、Now that we have a Validator, we can validate our bean by passing it to the validate method.
Any violations of the constraints defined in the User object will be returned as a Set:
By iterating over the violations, we can get all the violation messages using the getMessage method:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
2018-08-19 【设计模式】原则与案例