Spring boot 每个请求的Spring引导REST验证都失败
我已经在REST Spring引导API中实现了请求验证 以下是请求模型:Spring boot 每个请求的Spring引导REST验证都失败,spring-boot,Spring Boot,我已经在REST Spring引导API中实现了请求验证 以下是请求模型: @Data @ApiModel public class ConfirmUserTokenRequest { @Min(3) @Max(25) @NotBlank private String firstName; @Min(3) @Max(25) @NotBlank private String lastName; @Email @No
@Data
@ApiModel
public class ConfirmUserTokenRequest {
@Min(3)
@Max(25)
@NotBlank
private String firstName;
@Min(3)
@Max(25)
@NotBlank
private String lastName;
@Email
@NotBlank
private String email;
@Min(3)
@Max(50)
private String companyName;
@ApiModelProperty(value = "Usage purpose", allowableValues = "SEO, CRAWLING, BLOGGING, DEVOPS, OTHER")
private String usagePurpose;
@Min(2)
@Max(2)
@ApiModelProperty(value = "ISO Alpha-2 country code of user", example = "US")
private String countryCode;
@Min(6)
@Max(30)
@NotBlank
private String password;
@NotBlank
private String redirectUri;
private String data;
}
In the controller method, I have validated the request body parameter with *@Valid* annotation to turn on validation.
以下是控制器方法定义:
@PostMapping
@ResponseStatus(HttpStatus.NO_CONTENT)
public ResponseEntity<Object> createToken(@Valid @RequestBody ConfirmUserTokenRequest confirmUserTokenRequest)
我得到以下验证错误:
POST /no-auth/manager/token/confirm-user HTTP/1.1
Host: 127.0.0.1:8080
Content-Type: application/json
cache-control: no-cache
{
"firstName": "John",
"lastName": "Doe",
"email": "gayeta@example.com",
"countryCode": "AU",
"companyName": "Galaxy Corp",
"usagePurpose": "SEO",
"password": "init@test",
"data": null,
"redirectUri": "http://127.0.0.1:8080/callback/confirm-user"
}
{
"timestamp": "2019-01-09T06:54:56.635",
"message": "Validation failed",
"errors": [
"companyName: must be greater than or equal to 3",
"password: must be less than or equal to 30",
"lastName: must be less than or equal to 25",
"firstName: must be less than or equal to 25",
"lastName: must be greater than or equal to 3",
"password: must be greater than or equal to 6",
"firstName: must be greater than or equal to 3",
"companyName: must be less than or equal to 50"
]
}
即使对于这种看似有效的请求,也会抛出错误。出了什么问题?不要单独使用
@Min
和@Max
,而是尝试@Size(Min=2,Max=25)
此外,如果应用了
@Min
,则无需将@NotBlank
更改为@NotNull
@Min
和@Max
用于验证int、short、byte等数字字段及其各自的基本包装。
@Size
用于检查字段上的长度约束。
根据文档,
@Size
支持字符串、集合、映射和数组,而@Min
和@Max
支持原语及其包装器。不要单独使用@Min
和@Max
,请尝试@Size(Min=2,Max=25)
此外,如果应用了
@Min
,则无需将@NotBlank
更改为@NotNull
@Min
和@Max
用于验证int、short、byte等数字字段及其各自的基本包装。
@Size
用于检查字段上的长度约束。
根据文档,
@Size
支持字符串、集合、映射和数组,@Min
和@Max
支持原语及其包装器。请添加控制器层请添加准确回答问题的控制器层。这正好回答了问题。
{
"timestamp": "2019-01-09T06:54:56.635",
"message": "Validation failed",
"errors": [
"companyName: must be greater than or equal to 3",
"password: must be less than or equal to 30",
"lastName: must be less than or equal to 25",
"firstName: must be less than or equal to 25",
"lastName: must be greater than or equal to 3",
"password: must be greater than or equal to 6",
"firstName: must be greater than or equal to 3",
"companyName: must be less than or equal to 50"
]
}