当前位置:网站首页>@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
边栏推荐
- Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
- 当Steam教育进入个性化信息技术课程
- phpstudy小皮的mysql点击启动后迅速闪退,已解决
- AITM 2-0003 水平燃烧试验
- Prosci LAG-3 recombinant protein specification
- XML建模
- 《SAS编程和数据挖掘商业案例》学习笔记# 19
- Is it safe to open a stock account by mobile phone? My home is relatively remote. Is there a better way to open an account?
- Maker education infiltrating the transformation of maker spirit and culture
- 判断横竖屏的最佳实现
猜你喜欢
Norgen AAV extractant box instructions (including features)
解析五育融合之下的steam教育模式
研学旅游实践教育的开展助力文旅产业发展
Influence of oscilloscope probe on measurement bandwidth
Duchefa p1001 plant agar Chinese and English instructions
R语言【数据管理】
Clion configures Visual Studio (MSVC) and JOM multi-core compilation
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
随机推荐
树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
Test of incombustibility of cement adhesives BS 476-4
浅聊我和一些编程语言的缘分
木板ISO 5660-1 热量释放速率摸底测试
[case] Application of element display and hiding -- element mask
五层网络协议
概率论机器学习的先验知识(上)
驱动壳美国测试UL 2043 符合要求有哪些?
当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com
XML建模
Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
vant 源码解析之 utils/index.ts 工具函数
Duchefa cytokinin dihydrozeatin (DHZ) instructions
Mode - "Richter replacement principle"
ts 之 泛型
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
Write an interface based on flask
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
Introduction to TS, constructor and its this, inheritance, abstract class and interface
示波器探头对信号源阻抗的影响