当前位置:网站首页>@Validated basic parameter verification, grouping parameter verification and nested parameter verification
@Validated basic parameter verification, grouping parameter verification and nested parameter verification
2022-07-05 21:03:00 【Attacking brother pea】
1、@Validated Basic parameter verification
Entity class
@Data
public class User1Dto {
/** * user name */
@NotBlank(message = " The username cannot be empty !")
private String username;
/** * Gender */
@NotBlank(message = " Gender cannot be empty !")
private String gender;
/** * Age */
@Min(value = 1, message = " Wrong age !")
@Max(value = 120, message = " Wrong age !")
private int age;
/** * Address */
@NotBlank(message = " Address cannot be empty !")
private String address;
/** * mailbox */
@Email(message = " Error in mailbox !")
private String email;
/** * Phone number */
@Pattern(regexp = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$",message = " Wrong mobile number !")
private String mobile;
}
The control class
@RestController
@RequestMapping("/api/v1")
public class Demo1Controller {
@PostMapping("/insert")
public String validatedDemo1(@Validated @RequestBody Use1Dto use1Dto){
System.out.println(use1Dto);
return "success";
}
}
2、@Validated Group parameter verification
First step 、 Define a verification group class , Declare that the four interfaces correspond to different scenario verification
public class ValidationGroups {
public interface Select {
}
public interface Insert {
}
public interface Update {
}
public interface Detail {
}
public interface Delete{
}
}
The second step 、 Add verification rules and verification groups to the specific attributes of the entity class
@Data
public class Person {
@NotNull(message = "personId Not for null",groups = Select.class)
private Integer personId;
@NotEmpty(message = "name Can't be empty ",groups = Insert.class)
private String name;
private Integer age;
}
The third step 、 Create queries and new interfaces in the control layer , add to @Validated annotation , Assign groups , Can be more .
@GetMapping("/select")
public Object select(@Validated({
ValidationGroups.Select.class}) Person person) {
return person;
}
@GetMapping("/insert")
public Object insert(@Validated(ValidationGroups.Insert.class) Person person) {
return person;
}
3、@Validated Nested parameter validation
Entity class
@Data
public class Team1Dto {
/** * Team name */
@NotBlank(message = " Team name cannot be empty !")
private String name;
/** * Team personnel */
@NotNull(message = " Team personnel cannot be empty !")
@Valid
private List<User1Dto> userList;
/** * Team leader */
@NotNull(message = " Team leader cannot be empty !")
@Valid
private User1Dto user;
}
The control class
@RestController
@RequestMapping("/api/v1")
public class Demo1Controller {
@PostMapping("/insert")
public Result validatedDemo1(@Validated @RequestBody Use1Dto use1Dto){
return ResultUtil.success(use1Dto);
}
@PostMapping("/insert2")
public Result validatedDemo2(@Validated @RequestBody Team1Dto team1Dto){
return ResultUtil.success(team1Dto);
}
}
4、 Reference resources
@Validated Group check - A programmer's article
@Validated Annotations, , Group check , Nested verification ,@Valid and @Validated difference ,Spring Boot @Validated
Four 、@Validated Verify the grouping strategy
边栏推荐
- Is Kai Niu 2980 useful? Is it safe to open an account
- vant 源码解析 event.ts 事件处理 全局函数 addEventListener详解
- Pytorch实战——MNIST数据集手写数字识别
- Duchefa low melting point agarose PPC Chinese and English instructions
- 产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
- ODPs next map / reduce preparation
- hdu2377Bus Pass(构建更复杂的图+spfa)
- Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
- 当Steam教育进入个性化信息技术课程
- Aitm2-0002 12s or 60s vertical combustion test
猜你喜欢
当Steam教育进入个性化信息技术课程
珍爱网微服务底层框架演进从开源组件封装到自研
Duchefa d5124 md5a medium Chinese and English instructions
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
Prosci LAG-3 recombinant protein specification
LeetCode_哈希表_困难_149. 直线上最多的点数
haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践
XML建模
随机推荐
PHP deserialization +md5 collision
解读协作型机器人的日常应用功能
Abnova CRISPR spcas9 polyclonal antibody protocol
Determine the best implementation of horizontal and vertical screens
JS common method encapsulation
大二下个人发展小结
Duchefa d5124 md5a medium Chinese and English instructions
Mode - "Richter replacement principle"
Aitm2-0002 12s or 60s vertical combustion test
如何让化工企业的ERP库存账目更准确
LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
Analyze the knowledge transfer and sharing spirit of maker Education
php中explode函数存在的陷阱
Simple getting started example of Web Service
Enclosed please find. Net Maui's latest learning resources
Duchefa p1001 plant agar Chinese and English instructions
PHP反序列化+MD5碰撞
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
MYSQL IFNULL使用功能
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once