当前位置:网站首页>@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校验之分组策略
边栏推荐
- Using webassembly to operate excel on the browser side
- Sequence alignment
- js常用方法封装
- The development of research tourism practical education helps the development of cultural tourism industry
- Monorepo管理方法论和依赖安全
- 树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
- shell编程100例
- CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
- PHP反序列化+MD5碰撞
- POJ 3414 pots (bfs+ clues)
猜你喜欢
浅聊我和一些编程语言的缘分
Write an interface based on flask
Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
解析创客教育的知识迁移和分享精神
解析五育融合之下的steam教育模式
【案例】定位的运用-淘宝轮播图
The development of research tourism practical education helps the development of cultural tourism industry
Analysis of steam education mode under the integration of five Education
Learning robots have no way to start? Let me show you the current hot research directions of robots
ClickHouse 复制粘贴多行sql语句报错
随机推荐
The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
The development of research tourism practical education helps the development of cultural tourism industry
CareerCup它1.8 串移包括问题
【案例】定位的运用-淘宝轮播图
Clion-MinGW编译后的exe文件添加ico图标
实现浏览页面时校验用户是否已经完成登录的功能
Duchefa p1001 plant agar Chinese and English instructions
ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
Implementation of redis unique ID generator
The Chinese Academy of Management Sciences gathered industry experts, and Fu Qiang won the title of "top ten youth" of think tank experts
显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??
示波器探头对信号源阻抗的影响
Viewrootimpl and windowmanagerservice notes
SYSTEMd resolved enable debug log
示波器探头对测量带宽的影响
Monorepo管理方法论和依赖安全
Is it necessary for bazel to learn
Abnova blood total nucleic acid purification kit pre installed relevant instructions
培养机器人教育创造力的前沿科技