当前位置:网站首页>@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
边栏推荐
- Aitm2-0002 12s or 60s vertical combustion test
- ViewRootImpl和WindowManagerService笔记
- vant 源码解析 之深层 合并对象 深拷贝
- SYSTEMd resolved enable debug log
- bazel是否有学习的必要
- 2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
- SQL series (basic) - Chapter 2 limiting and sorting data
- 显示屏DIN 4102-1 Class B1防火测试要求
- Talk about my fate with some programming languages
- 解析五育融合之下的steam教育模式
猜你喜欢

浅聊我和一些编程语言的缘分

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

PHP反序列化+MD5碰撞

Norgen AAV extractant box instructions (including features)

示波器探头对信号源阻抗的影响

PVC plastic sheets BS 476-6 determination of flame propagation properties

Use of thread pool

基于AVFoundation实现视频录制的两种方式

Enclosed please find. Net Maui's latest learning resources

Analyze the knowledge transfer and sharing spirit of maker Education
随机推荐
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
显示屏DIN 4102-1 Class B1防火测试要求
Binary search
Chemical properties and application instructions of prosci Lag3 antibody
Cutting edge technology for cultivating robot education creativity
Traps in the explode function in PHP
MySQL ifnull usage function
When steam education enters personalized information technology courses
Careercup its 1.8 serial shift includes problems
当Steam教育进入个性化信息技术课程
poj 3414 Pots (bfs+线索)
Pytoch practice -- MNIST dataset handwritten digit recognition
国外LEAD美国简称对照表
SYSTEMd resolved enable debug log
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?
Duchefa d5124 md5a medium Chinese and English instructions
Aitm2-0002 12s or 60s vertical combustion test
Open source SPL eliminates tens of thousands of database intermediate tables
Prior knowledge of machine learning in probability theory (Part 1)
五层网络协议