当前位置:网站首页>@The real difference and usage between validated and @valid
@The real difference and usage between validated and @valid
2022-07-26 10:47:00 【Eric-x】
@Validated and @Valid There are three main differences : Annotation position 、 grouping 、 Nested verification
summary
- @Valid It's using Hibernate validation When you use
- @Validated It's just Spring Validato r The verification mechanism uses
explain :java Of JSR303 The statement @Valid This kind of interface , and Hibernate-validator It has been realized
@Validation Yes @Valid Second encapsulation is carried out , There is no difference in use , But in groups 、 Annotation position 、 Nested verification and other functions are different , Here we mainly explain these situations .
Annotation position
- @Validated: Used in type 、 Method and method parameters . But it cannot be used for member properties (field)
- @Valid: It can be used in methods 、 Constructors 、 Method parameters and member properties (field) On
If @Validated Annotations on member properties , Will not apply to field error
Group check
- @Validated: Provide grouping function , It can be used in parameter verification , Different validation mechanisms according to different groups
- @Valid: No grouping function
Nested verification
One to be verified pojo class , It also contains the objects to be verified , You need to annotate the object to be verified @Valid, To verify the member properties in the object to be verified , Not available here @Validated.
give an example :
Constraint verification is required bean:
public class TeacherBean {
@NotEmpty(message = " Teacher name cannot be empty ")
private String teacherName;
@Min(value = 1, message = " The types of subjects are from 1 Start calculating ")
private int type;
}
public class StudentBean implements Serializable{
@NotBlank(message = " The username cannot be empty ")
private String name;
@Min(value = 18, message = " Age cannot be less than 18 year ")
private Integer age;
@Pattern(regexp = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$", message = " Mobile number format error ")
private String phoneNum;
@Email(message = " Mailbox format error ")
private String email;
@MyConstraint
private String className;
@NotNull(message = " The teacher cannot be empty ")
@Size(min = 1, message = " At least one teacher ")
private List<TeacherBean> teacherBeans;
}
Be careful :
Here to teacherBeans Only verified NotNull, and Size, It did not teacher Check the fields in the information ,
therefore , You usually need to @Valid add , as follows
@Valid
@NotNull(message = " The teacher cannot be empty ")
@Size(min = 1, message = " At least one teacher ")
private List<TeacherBean> teacherBeans;
Reference link :https://mp.weixin.qq.com/s/vErmG-x8zSYJoNCWLovndg
边栏推荐
- RT thread learning notes (VIII) -- start the elmfat file system based on SPI flash (Part 2)
- flutter dart生成N个区间范围内不重复随机数List
- Anaconda is used on vscode (the environment has been configured)
- Sword finger offer (twenty): stack containing min function
- C language pengge 20210812c language function
- 面试知识点
- Build ARM embedded development environment
- Successfully transplanted stemwin v5.22 on Shenzhou IV development board
- 剑指Offer(四十三):左旋转字符串
- Sql Server 数据库之初学体验
猜你喜欢

按二进制数中1的个数分类

Kali view IP address
![[leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits](/img/de/62fca587cde95110c2a967ca93eea5.png)
[leetcode daily question 2021/2/18] [detailed explanation] minimum number of turns of 995. K continuous bits

Issue 7: how do you choose between curling up and lying flat

一文详解Nodejs中fs文件模块与path路径模块

在altium designer中禁用USBJATG

为什么需要自动化测试?软件测试师带你测评不同软件测试工具

putty的使用教程

2021-08-12函数递归_和鹏哥学习C语言

2021-08-12 function recursion_ Learn C language with brother Peng
随机推荐
剑指Offer(二十一):栈的压入、弹出序列
shell 脚本 失败自动重复执行
[leetcode daily question 2021/4/29]403. Frogs cross the river
el-table实现可编辑表格
Pengge C language sixth class
Sword finger offer (43): left rotation string
c结构体中定义的成员指针赋值与结构体指针作为成员函数参数的使用
RT-Thread 学习笔记(七)---开启基于SPI Flash的elmfat文件系统(中)
反射机制简述
剑指Offer(十):矩形覆盖
鹏哥C语言20210811程序结构作业
RT-Thread 学习笔记(三)---用SCons 构建编译环境
Sword finger offer (44): flip the word order sequence
剑指Offer(五十二):正则化表达式
Issue 7: how do you choose between curling up and lying flat
RT-Thread 学习笔记(一)---配置RT-Thread开发环境
鹏哥C语言第七节课总结
MySQL速学-2021-09-01
Pengge C language - minesweeping 2021-08-16
[machine learning notes] [style transfer] deeplearning ai course4 4th week programming(tensorflow2)