当前位置:网站首页>@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
边栏推荐
- 微信公众号开发 获取openid时报错40029 invalid code 问题的解决
- 2021-08-14三子棋
- PLC与伺服电机连接
- [paper after dinner] deep mining external perfect data for chestx ray disease screening
- 剑指Offer(四十三):左旋转字符串
- Sword finger offer (53): a string representing a numeric value
- 鹏哥C语言20210811程序结构作业
- 如何组装一个注册中心?
- Flutter CachedNetworkImage圆角
- 1837.K进制表示下的各位数字总和
猜你喜欢

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

在神州IV开发板上成功移植STemWin V5.22

用两个栈实现队列

RT thread learning notes (VII) -- open the elmfat file system based on SPI flash (middle)
![[machine learning notes] [face recognition] deeplearning ai course4 4th week programming](/img/7e/9c0e88097b90c0c24ebf86f090805b.png)
[machine learning notes] [face recognition] deeplearning ai course4 4th week programming

如何组装一个注册中心?
![[leetcode daily question 2021/5/8]1723. The shortest time to complete all work](/img/e7/a48bb5b8a86cbc4cd5b37bb16661a8.png)
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work

在altium designer中禁用USBJATG

RT thread learning notes (III) -- building a compilation environment with scons

Issue 6: which mainstream programming language should college students choose
随机推荐
剑指Offer(四十三):左旋转字符串
Flutter CachedNetworkImage圆角
Flutter TextField怎样去除下划线及有焦点时颜色
剑指Offer(五十二):正则化表达式
如何组装一个注册中心?
20210807 1 c language program structure
Sword finger offer (53): a string representing a numeric value
Sword finger offer (44): flip the word order sequence
在altium designer中禁用USBJATG
[leetcode daily question 2021/4/23]368. Maximum divisible subset
2021-08-12 function recursion_ Learn C language with brother Peng
Successfully transplanted stemwin v5.22 on Shenzhou IV development board
27.移除元素
鹏哥C语言——扫雷2021-08-16
Sword finger offer (49): convert a string to an integer
Issue 7: how do you choose between curling up and lying flat
创建EOS账户 Action
剑指Offer(四十四):翻转单词顺序序列
MFC中0x003b66c3 处有未经处理的异常: 0xC000041D: 用户回调期间遇到未经处理的异常
Oracle create index