java Spring 启动验证注释 @Valid 和 @NotBlank 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48614773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot validation annotations @Valid and @NotBlank not working
提问by Poonkodi Sivapragasam
Given below is my main controller from which i am calling getPDFDetails method.
下面给出的是我的主控制器,我从中调用 getPDFDetails 方法。
@RequestMapping(value=PATH_PRINT_CONTRACTS, method=RequestMethod.POST)
public ResponseEntity<?> printContracts(@RequestBody final UpdatePrintContracts updatePrintContracts) throws Exception {
System.out.println("contracts value is "+ updatePrintContracts);
Integer cancellationReasons = service.getPDFDetails(updatePrintContracts);
System.out.println("success!");
return ResponseEntity.ok(cancellationReasons);
}
Below is the UpdatePrintContracts class where i have defined all the variables with validation annotations and corresponding getter/setter methods.
下面是 UpdatePrintContracts 类,我在其中定义了带有验证注释和相应的 getter/setter 方法的所有变量。
public class UpdatePrintContracts {
@Valid
@NotBlank
@Pattern(regexp = "\p{Alnum}{1,30}")
String isReprint;
@Valid
@NotBlank
Integer dealerId;
@Valid
@NotBlank
@Pattern(regexp = "\p{Alnum}{1,30}")
String includeSignatureCoordinates;
@Valid
@NotBlank
java.util.List<Integer> contractNumbers;
public String getIsReprint() {
return isReprint;
}
public void setIsReprint(String isReprint) {
this.isReprint = isReprint;
}
public Integer getDealerId() {
return dealerId;
}
public void setDealerId(Integer dealerId) {
this.dealerId = dealerId;
}
public String getIncludeSignatureCoordinates() {
return includeSignatureCoordinates;
}
public void setIncludeSignatureCoordinates(String includeSignatureCoordinates) {
this.includeSignatureCoordinates = includeSignatureCoordinates;
}
public java.util.List<Integer> getContractNumbers() {
return contractNumbers;
}
public void setContractNumbers(java.util.List<Integer> contractNumbers) {
this.contractNumbers = contractNumbers;
}
}
I am trying to Run the application as Spring boot app by right clicking on the project (Run As) and passing blank values for variables isReprint and includeSignatureCoordinates thru Soap UI. However the validation doesn't seem to work and not throwing any validation error on the Soap UI. What am i missing? Any help is appreciated!
我试图通过右键单击项目(运行方式)并通过 Soap UI 传递变量 isReprint 和 includeSignatureCoordinates 的空白值来将应用程序作为 Spring 启动应用程序运行。但是,验证似乎不起作用,并且不会在 Soap UI 上引发任何验证错误。我错过了什么?任何帮助表示赞赏!
回答by surya
First you dont need to have @Valid annotation for those class variables in UpdatePrintContracts . You can delete them.
首先,您不需要为 UpdatePrintContracts 中的那些类变量添加 @Valid 注释。您可以删除它们。
To trigger validation of a @Controller input, simply annotate the input argument as @Valid or @Validated:
要触发 @Controller 输入的验证,只需将输入参数注释为 @Valid 或 @Validated:
@RequestMapping(value=PATH_PRINT_CONTRACTS, method=RequestMethod.POST)
public ResponseEntity<?> printContracts(@Valid @RequestBody final UpdatePrintContracts updatePrintContracts) throws Exception {
Refer herefor full understanding of validating models in spring boot.
请参阅此处以全面了解 Spring Boot 中的验证模型。
And If you want to check that a string contains only specific characters, you must add anchors (^ for beginning of the string, $ for end of the string) to be sure that your pattern matches all the string.Curly brackets are only to write a quantity,
如果你想检查一个字符串是否只包含特定的字符,你必须添加锚点(^ 表示字符串的开头,$ 表示字符串的结尾)以确保您的模式匹配所有字符串。大括号只写一个数量,
@Pattern(regexp = "^[\p{Alnum}]{1,32}$")
Lastly i assume you have following jars in your classpath,
最后,我假设您的类路径中有以下 jars,
.validation-api.jar (contains the abstract API and the annotation scanner)
.validation-api.jar(包含抽象API和注解扫描器)
.hibernate-validator.jar (contains the concrete implementation)
.hibernate-validator.jar(包含具体实现)
回答by Deb
If you are facing this problem in latest version of spring boot (2.3.0) make sure to add the following dependency:
如果您在最新版本的 spring boot (2.3.0) 中遇到此问题,请确保添加以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Observation:In earlier version of Spring Boot (1.4.7), javax.validation
used to work out of the box. But, after upgrading to latest version, annotations broke. Adding the following dependency alone doesn't work:
观察:在较早版本的 Spring Boot (1.4.7) 中,javax.validation
用于开箱即用。但是,升级到最新版本后,注释坏了。单独添加以下依赖项不起作用:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
Because this provides JSR Specification but not the implementation. You can also use hibernate-validator
instead of spring-boot-starter-validation
.
因为这提供了 JSR 规范而不是实现。您也可以使用hibernate-validator
代替spring-boot-starter-validation
。