最近项目刚起,SpringBoot
的,开始做了个参数校验
,这里就记录以下。
关注博主不迷路,获取更多干货资源
1 POM
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> <version>2.5.3</version> </dependency>
|
2 Controller
| @GetMapping(value = "allDatabases", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseInfo getAllDatabases(@Validated(SourceInfo.GetAllDatabases.class) @RequestBody SourceInfo sourceInfo) { return (ResponseInfo) sourceMetadataService.getAllDatabases(sourceInfo); }
|
2.1 参数解释
@Validated
:这里使用@Validated
而不是@Valid
,因为实体SourceInfo
里需要做分组校验
,@Valid做不了。
SourceInfo.GetAllDatabases.class
:这个是级联校验的分组
,后面实体里看一眼就知道了。
3 实体
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| @Data @Accessors(chain = true) public class SourceInfo { private String sourceCode; private String sourceName; @NotBlank(message = "sourceType cannot be blank") private String sourceType; private String sourceDes;
@Valid private List<SourceConfInfo> sourceConfInfos; @Valid private SourceOptInfo sourceOptInfo;
public interface CreateTable extends Default { }
public interface DropTable extends Default { }
public interface ExecuteSql extends Default { }
public interface GetAllDatabases extends Default { }
public interface GetTargetDatabaseAllTables extends Default { }
public interface GetTargetTableFields extends Default { }
}
|
| @Data @Accessors(chain = true) public class SourceConfInfo { @NotBlank(message = "confName cannot be blank") private String confName; @NotBlank(message = "confValue cannot be blank") private String confValue; private String confDes; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Data @Accessors(chain = true) public class SourceOptInfo {
@NotEmpty(message = "databases cannot be empty", groups = {SourceInfo.CreateTable.class, SourceInfo.DropTable.class, SourceInfo.GetTargetDatabaseAllTables.class, SourceInfo.GetTargetTableFields.class, SourceInfo.ExecuteSql.class}) private List<String> databases;
@NotEmpty(message = "databases cannot be empty", groups = {SourceInfo.CreateTable.class, SourceInfo.ExecuteSql.class}) private List<String> sqls;
@NotEmpty(message = "databases cannot be empty", groups = {SourceInfo.DropTable.class, SourceInfo.GetTargetTableFields.class}) private List<String> tables; }
|
4 支持的注解
我只简单列一下,具体的打开源码去看吧。
注解 |
解释 |
@AssertFalse |
带注释的元素必须为 false。 支持的类型是boolean和Boolean 。
null元素被认为是有效的。 |
@AssertTrue |
带注释元素必须为true。 支持的类型是boolean和Boolean 。
null元素被认为是有效的。 |
@DecimalMax |
带注释的元素必须是一个数字,其值必须小于或等于指定的最大值。 |
@DecimalMin |
带注释的元素必须是一个数字,其值必须大于或等于指定的最小值。 |
@Digits |
带注释元素必须是可接受范围内的数字。 |
@Email |
该字符串必须是格式正确的电子邮件地址。 构成有效电子邮件地址的确切语义留给 Jakarta Bean 验证提供程序。 接受CharSequence 。
null元素被认为是有效的。 |
@Future |
带注释元素必须是未来的某个时刻、日期或时间。 |
@FutureOrPresent |
带注释元素必须是现在或将来的瞬间、日期或时间。 |
@Max |
带注释的元素必须是一个数字,其值必须小于或等于指定的最大值。 |
@Min |
带注释的元素必须是一个数字,其值必须大于或等于指定的最小值。 |
@Negative |
带注释的元素必须是严格的负数(即 0 被视为无效值) |
@NegativeOrZero |
带注释的元素必须是负数或 0。 |
@NotBlank |
带注释的元素不能为null并且必须至少包含一个非空白字符。(一般字符串用这个) |
@NotEmpty |
带注释的元素不得为null或为空。(一般数组用这个) |
@NotNull |
带注释的元素不能为null 。 接受任何类型。 |
@Null |
带注释的元素必须为null 。 接受任何类型。 |
@Past |
带注释的元素必须是过去的某个时刻、日期或时间。 |
@PastOrPresent |
带注释元素必须是过去或现在的瞬间、日期或时间。 |
@Pattern |
带注释的CharSequence必须匹配指定的正则表达式。 |
@Positive |
带注释元素必须是严格的正数(即 0 被视为无效值)。 |
@PositiveOrZero |
带注释元素必须是正数或 0。 |
@Size |
带注释的元素大小必须在指定的边界(包括)之间。 |
5 完工
吃饭饭
,躺板板
。
关注博主不迷路