当前位置:网站首页>@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校验之分组策略
边栏推荐
- 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
- Viewrootimpl and windowmanagerservice notes
- Écrire une interface basée sur flask
- Cutting edge technology for cultivating robot education creativity
- sql系列(基础)-第二章 限制和排序数据
- 教你自己训练的pytorch模型转caffe(二)
- Abnova DNA marker high quality control test program
- Monorepo管理方法论和依赖安全
- Is Kai Niu 2980 useful? Is it safe to open an account
- Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
猜你喜欢

Analyze the knowledge transfer and sharing spirit of maker Education

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

XML建模

珍爱网微服务底层框架演进从开源组件封装到自研

The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application

PHP deserialization +md5 collision

中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097

Chemical properties and application instructions of prosci Lag3 antibody

解析五育融合之下的steam教育模式

木板ISO 5660-1 热量释放速率摸底测试
随机推荐
国外LEAD美国简称对照表
ts 之 属性的修饰符public、private、protect
Clion-MinGW编译后的exe文件添加ico图标
SQL series (basic) - Chapter 2 limiting and sorting data
基于AVFoundation实现视频录制的两种方式
解析五育融合之下的steam教育模式
显示屏DIN 4102-1 Class B1防火测试要求
Mode - "Richter replacement principle"
获取前一天的js(时间戳转换)
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
MYSQL IFNULL使用功能
解析创客教育的知识迁移和分享精神
Modifiers of attributes of TS public, private, protect
启牛2980有没有用?开户安全吗、
Duchefa cytokinin dihydrozeatin (DHZ) instructions
Using webassembly to operate excel on the browser side
Is Kai Niu 2980 useful? Is it safe to open an account
wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
Simple getting started example of Web Service
Comparison table of foreign lead American abbreviations