当前位置:网站首页>@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
边栏推荐
- Add ICO icon to clion MinGW compiled EXE file
- 基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
- Enclosed please find. Net Maui's latest learning resources
- 显示屏DIN 4102-1 Class B1防火测试要求
- MySQL fully parses json/ arrays
- sql系列(基础)-第二章 限制和排序数据
- @Validated基础参数校验、分组参数验证和嵌套参数验证
- Write an interface based on flask
- Is Kai Niu 2980 useful? Is it safe to open an account
- Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
猜你喜欢
请查收.NET MAUI 的最新学习资源
PVC 塑料片BS 476-6 火焰传播性能测定
XML modeling
Using webassembly to operate excel on the browser side
示波器探头对测量带宽的影响
Duchefa cytokinin dihydrozeatin (DHZ) instructions
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
Abnova maxpab mouse derived polyclonal antibody solution
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
五层网络协议
随机推荐
Careercup its 1.8 serial shift includes problems
Monorepo management methodology and dependency security
AITM2-0002 12s或60s垂直燃烧试验
最长摆动序列[贪心练习]
XML modeling
当Steam教育进入个性化信息技术课程
MySQL fully parses json/ arrays
ClickHouse 复制粘贴多行sql语句报错
systemd-resolved 开启 debug 日志
Popular science | does poor English affect the NPDP exam?
判断横竖屏的最佳实现
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
poj 3414 Pots (bfs+线索)
Add ICO icon to clion MinGW compiled EXE file
js常用方法封装
MySQL 千万数据量深分页优化, 拒绝线上故障!
Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
Duchefa d5124 md5a medium Chinese and English instructions
How to renew NPDP? Here comes the operation guide!
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once