疾速开始
1. 引入 Validation 依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId></dependency>
2. 编写校验代码
@Data 为 lombok 的注解,在此不做具体阐明。
@Datapublic class UserRequest { @NotBlank private String name; @Min(0) @Max(200) private Integer age;}
3. 申明校验参数
@RestControllerpublic class UserController { @PostMapping("/api/users") public UserRequest createUser(@RequestBody @Validated UserRequest userRequest) { return userRequest; }}
4. 捕获全局异样
这一步不是必须的,目标是为了能不便查看校验后果。
@ControllerAdvicepublic class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<Map<String, Object>> handleMethodArgumentNotValidException( MethodArgumentNotValidException exception) { List<String> errorMessages = exception.getFieldErrors() .stream() .map(fieldError -> String.format("%s %s", fieldError.getField(), fieldError.getDefaultMessage())) .sorted() .collect(Collectors.toList()); Map<String, Object> errorMessageMap = CollectionUtils.newLinkedHashMap(2); errorMessageMap.put("code", HttpStatus.BAD_REQUEST.value()); errorMessageMap.put("message", errorMessages); return ResponseEntity.badRequest().body(errorMessageMap); }}
5. 查看校验成果
应用 curl 拜访接口:
curl --location --request POST 'http://localhost:8080/api/users' \--header 'Content-Type: application/json' \--data-raw '{ "name": "", "age": -1}'
最终的校验后果如下:
{ "code": 400, "message": [ "age 最小不能小于0", "name 不能为空" ]}
罕用注解
@NotNull、@NotEmpty 与 @NotBlank
注解 | 作用对象 | 含意 |
---|---|---|
@NotNull | 能够作用于任意对象 | 对象不能为 null |
@NotEmpty | 只能作用于 String、Collection、Map 和数组 | 对象不能为 null ,且对象的长度不能为 0 |
@NotBlank | 只能作用于 String | 字符串不能为 null ,且字符串的长度不能为 0 |
示例代码:
@Datapublic class NotAnnotationRequest { @NotNull private Object notNullObject; @NotEmpty private String notEmptyString; @NotEmpty private Collection<Integer> notEmptyCollection; @NotEmpty private Map<String, String> notEmptyMap; @NotEmpty private int[] notEmptyArray; @NotBlank private String notBlankString;}
@Size 与 @Min、@Max
注解 | 作用对象 | 含意 |
---|---|---|
@Size | 能够作用于 String、Collection、Map 和数组 | 蕴含两个变量:min 和 max,其中 min 的默认值为 0 ,示意对象的最小长度;max 的默认值为 Integer.MAX_VALUE ,示意对象的最大长度 |
@Min | 能够作用于 BigDecimal、BigInteger,byte、short、int、long 以及它们的包装类型 | 示意数字的最小值 |
@Max | 能够作用于 BigDecimal、BigInteger,byte、short、int、long 以及它们的包装类型 | 示意数字的最大值 |
示例代码:
@Datapublic class RangeAnnotationRequest { @Size(min = 1, max = 5) private String limitedString; @Size(min = 1, max = 5) private Collection<Integer> limitedCollection; @Size(min = 1, max = 5) private Map<String, String> limitedMap; @Size(min = 1, max = 5) private int[] limitedArray; @Min(1) @Max(2) private Integer limitedNumber;}
@Pattern
参数 | 含意 |
---|---|
regexp | 正则表达式 |
flags | 标识正则表达式的模式,蕴含:<br/>Pattern.Flag.UNIX_LINES、<br/>Pattern.Flag.CASE_INSENSITIVE、<br/>Pattern.Flag.COMMENTS、<br/>Pattern.Flag.MULTILINE、<br/>Pattern.Flag.DOTALL、<br/>Pattern.Flag.UNICODE_CASE、<br/>Pattern.Flag.CANON_EQ<br/>共7种模式 |
message | 谬误提示信息(message 不是 @Pattern 特有的,所有 Validation 注解都蕴含该参数) |
示例代码:
@Datapublic class PatternAnnotationRequest { @Pattern(regexp = "^1[3456789]\\d{9}$", message = "手机号码格局不正确") private String mobile;}
@Valid 与 @Validated
@Valid 和 @Validated 都能够用来申明校验参数,但它们并不齐全一样,它们的区别次要蕴含以下几个方面:
- @Valid 是定义在 javax 包下的规范注解,@Validated 是 Spring 官网定义的注解。
- @Valid 能够作用于办法、字段、构造函数、参数和运行时应用的类型,@Validated 可作用于类型、办法和参数。
- @Valid 能够用来定义嵌套校验,@Validated 无奈独自实现嵌套校验的定义。
- @Validated 反对分组校验,@Valid 不反对分组校验。
应用 @Valid 申明校验参数
@RestControllerpublic class UserController { @PostMapping("/api/users") public UserRequest createUser(@RequestBody @Valid UserRequest userRequest) { return userRequest; }}
嵌套校验
编写校验代码:
@Datapublic class CompanyRequest { @NotBlank private String name; @Valid @NotNull private AddressRequest address;}
@Datapublic class AddressRequest { @NotBlank private String country; @NotBlank private String province; @NotBlank private String city;}
申明校验参数:
@RestControllerpublic class NestValidController { @PostMapping("/api/companies") public CompanyRequest createCompany(@RequestBody @Validated CompanyRequest companyRequest) { return companyRequest; }}
分组校验
定义分组:
public interface CreateGroup extends Default {}
public interface UpdateGroup extends Default {}
编写校验代码:
@Datapublic class OrganizationRequest { @NotBlank(groups = CreateGroup.class) @Null(groups = UpdateGroup.class) private String orgCode; @NotBlank private String orgName;}
申明校验参数:
@RestControllerpublic class GroupValidController { @PostMapping("/api/organizations") public OrganizationRequest createOrganization( @RequestBody @Validated(CreateGroup.class) OrganizationRequest organizationRequest) { return organizationRequest; } @PutMapping("/api/organizations") public OrganizationRequest updateOrganization( @RequestBody @Validated(UpdateGroup.class) OrganizationRequest organizationRequest) { return organizationRequest; }}
其余注解
@Null
注解 | 作用对象 | 含意 |
---|---|---|
@Null | 能够作用于任意对象 | 对象必须为 null |
示例代码:
@Datapublic class NullAnnotationRequest { @Null private Object nullObject;}
注解 | 作用对象 | 含意 |
---|---|---|
只能作用于 String | 字符串必须为非法的 email |
示例代码:
@Datapublic class EmailAnnotationRequest { @Email private String email;}
布尔类型注解
注解 | 作用对象 | 含意 |
---|---|---|
@AssertTrue | 能够作用于 boolean 和 Boolean | 对象必须为 true |
@AssertFalse | 能够作用于 boolean 和 Boolean | 对象必须为 false |
示例代码:
@Datapublic class BooleanAnnotationRequest { @AssertTrue private Boolean trueParam; @AssertFalse private boolean falseParam;}
数字类型注解
注解 | 作用对象 | 含意 |
---|---|---|
@DecimalMin | 能够作用于数字类型和字符串 | 示意数字的最小值 |
@DecimalMax | 能够作用于数字类型和字符串 | 示意数字的最大值 |
@Digits | 能够作用于数字类型和字符串 | 蕴含两个变量:integer 和 fraction,其中 integer 示意整数位的最大位数;fraction 示意小数位的最大位数 |
@Negative | 能够作用于数字类型 | 示意必须为正数 |
@NegativeOrZero | 能够作用于数字类型 | 示意必须为正数或零 |
@Positive | 能够作用于数字类型 | 示意必须为负数 |
@PositiveOrZero | 能够作用于数字类型 | 示意必须为负数或零 |
示例代码:
@Datapublic class NumberAnnotationRequest { @DecimalMin("1.5") @DecimalMax("2.5") private BigDecimal limitedBigDecimal; @Digits(integer = 1, fraction = 2) private BigDecimal digits; @Negative private short negative; @NegativeOrZero private int negativeOrZero; @Positive private long positive; @PositiveOrZero private BigDecimal positiveOrZero;}
工夫类型注解
注解 | 作用对象 | 含意 |
---|---|---|
@Past | 能够作用于工夫类型 | 示意必须为过来的工夫 |
@PastOrPresent | 能够作用于工夫类型 | 示意必须为过来的工夫或当初 |
@Future | 能够作用于工夫类型 | 示意必须为未来的工夫 |
@FutureOrPresent | 能够作用于工夫类型 | 示意必须为未来的工夫或当初 |
示例代码:
@Datapublic class TimeAnnotationRequest { @Past private Date past; @PastOrPresent private Date pastOrPresent; @Future private Date future; @FutureOrPresent private Date futureOrPresent;}
获取源码