当前位置:网站首页>@Validated基础参数校验、分组参数验证和嵌套参数验证
@Validated基础参数校验、分组参数验证和嵌套参数验证
2022-07-05 21:00:00 【进击的豌豆哥】
1、@Validated基础参数校验
实体类
@Data
public class User1Dto {
/** * 用户名 */
@NotBlank(message = "用户名不能为空!")
private String username;
/** * 性别 */
@NotBlank(message = "性别不能为空!")
private String gender;
/** * 年龄 */
@Min(value = 1, message = "年龄有误!")
@Max(value = 120, message = "年龄有误!")
private int age;
/** * 地址 */
@NotBlank(message = "地址不能为空!")
private String address;
/** * 邮箱 */
@Email(message = "邮箱有误!")
private String email;
/** * 手机号码 */
@Pattern(regexp = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$",message = "手机号码有误!")
private String mobile;
}
控制类
@RestController
@RequestMapping("/api/v1")
public class Demo1Controller {
@PostMapping("/insert")
public String validatedDemo1(@Validated @RequestBody Use1Dto use1Dto){
System.out.println(use1Dto);
return "success";
}
}
2、@Validated分组参数校验
第一步、定义一个校验组类,声明四个接口对应不同场景校验
public class ValidationGroups {
public interface Select {
}
public interface Insert {
}
public interface Update {
}
public interface Detail {
}
public interface Delete{
}
}
第二步、在实体类具体属性添加校验规则及校验分组
@Data
public class Person {
@NotNull(message = "personId不能为null",groups = Select.class)
private Integer personId;
@NotEmpty(message = "name不能为空",groups = Insert.class)
private String name;
private Integer age;
}
第三步、在控制层创建查询和新增接口,添加@Validated注解,指定分组,可多个。
@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嵌套参数验证
实体类
@Data
public class Team1Dto {
/** * 队伍名称 */
@NotBlank(message = "队伍名称不能为空!")
private String name;
/** * 队伍人员 */
@NotNull(message = "队伍人员不能为空!")
@Valid
private List<User1Dto> userList;
/** * 队伍负责人 */
@NotNull(message = "队伍负责人不能为空!")
@Valid
private User1Dto user;
}
控制类
@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、参考
@Validated分组校验 - 一只程序员的文章
@Validated注解详解,分组校验,嵌套校验,@Valid和@Validated 区别,Spring Boot @Validated
四、@Validated校验之分组策略
边栏推荐
- 渗透创客精神文化转化的创客教育
- bazel是否有学习的必要
- 使用WebAssembly在浏览器端操作Excel
- Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
- 2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
- LeetCode: Distinct Subsequences [115]
- 【案例】元素的显示与隐藏的运用--元素遮罩
- Learning notes of SAS programming and data mining business case 19
- XML建模
- CLion配置visual studio(msvc)和JOM多核编译
猜你喜欢
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
Norgen AAV extractant box instructions (including features)
ClickHouse 复制粘贴多行sql语句报错
leetcode:1139. 最大的以 1 为边界的正方形
Cutting edge technology for cultivating robot education creativity
Interpreting the daily application functions of cooperative robots
ArcGIS栅格重采样方法介绍
Abnova maxpab mouse derived polyclonal antibody solution
Write an interface based on flask
Open source SPL eliminates tens of thousands of database intermediate tables
随机推荐
Abnova CRISPR spcas9 polyclonal antibody protocol
XML modeling
Pytorch实战——MNIST数据集手写数字识别
教你自己训练的pytorch模型转caffe(二)
LeetCode_哈希表_困难_149. 直线上最多的点数
水泥胶黏剂BS 476-4 不燃性测试
Binary search
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
PHP反序列化+MD5碰撞
php中explode函数存在的陷阱
Clear app data and get Icon
Modifiers of attributes of TS public, private, protect
Material design component - use bottomsheet to show extended content (II)
hdu2377Bus Pass(构建更复杂的图+spfa)
ODPs next map / reduce preparation
Duchefa cytokinin dihydrozeatin (DHZ) instructions
如何让化工企业的ERP库存账目更准确
Popular science | does poor English affect the NPDP exam?
Abnova e (diii) (WNV) recombinant protein Chinese and English instructions
显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??