Spring boot validation annotations @Valid and @NotBlank not working

Poo*_*sam 2 java validation soapui spring-boot

Given below is my main controller from which i am calling getPDFDetails method.

@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);
    }   
Run Code Online (Sandbox Code Playgroud)

Below is the UpdatePrintContracts class where i have defined all the variables with validation annotations and corresponding getter/setter methods.

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

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!

Deb*_*Deb 73

如果您在最新版本的 spring boot (2.3.0) 中遇到此问题,请确保添加以下依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

观察: 在较早版本的 Spring Boot (1.4.7) 中,javax.validation用于开箱即用。但是,升级到最新版本后,注释坏了。单独添加以下依赖项不起作用:

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

因为这提供了 JSR 规范而不是实现。您也可以使用hibernate-validator代替spring-boot-starter-validation

  • 添加 spring-boot-starter-validation 解决了我的问题。谢谢你! (6认同)

fir*_*ter 24

我遇到了同样的错误。

我必须单独使用以下两个依赖项:

      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

并在其余控制器级别使用@Validated注释(import org.springframework.validation.annotation.Validated),并@Valid在方法参数级别使用注释(import javax.validation.Valid)

如果还有任何其他额外的依赖项,如javax.validation.validation-apiorg.hibernate.hibernate-validator等,那么验证对我来说就停止工作了。因此,请确保从 pom.xml 中删除这些依赖项


小智 16

对于在2.0.1.Final 中遇到此问题的任何人:

在 2.2 以上的所有 SpringBoot 版本中,Validations starter 不再是 web starter 的一部分

在此处检查注释

所以,你所要做的就是在你的 build.gradle/pom 文件中添加这个依赖项

等级:

implementation 'org.springframework.boot:spring-boot-starter-validation'
Run Code Online (Sandbox Code Playgroud)

行家

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)


小智 11

确保在@RequestBody之前使用@Valid注解

对于较新版本的 Spring Boot,请确保所有验证注释都是从jakarta.validation.*包中选取的,而不是从javax.validation.*. 由于两者的注释名称相同。


小智 8

这适用于在执行上述步骤后仍然遇到相同问题的任何人。我必须重新启动 IDE (IntelliJ) 才能使更改生效。


sur*_*rya 7

首先,在UpdatePrintContracts中,这些类变量不需要@Valid批注。您可以删除它们。

要触发@Controller输入的验证,只需将输入参数注释为@Valid或@Validated:

@RequestMapping(value=PATH_PRINT_CONTRACTS, method=RequestMethod.POST)
public ResponseEntity<?> printContracts(@Valid @RequestBody  final UpdatePrintContracts updatePrintContracts) throws Exception {
Run Code Online (Sandbox Code Playgroud)

请参考此处以全面了解在弹簧靴中验证模型的知识。

并且,如果要检查字符串是否仅包含特定字符,则必须添加锚点(^表示字符串的开头,$表示字符串的结尾),以确保您的模式与所有字符串都匹配。数量

@Pattern(regexp = "^[\\p{Alnum}]{1,32}$")
Run Code Online (Sandbox Code Playgroud)

最后,我假设您在类路径中有以下jar,

.validation-api.jar(包含抽象API和注释扫描器)

.hibernate-validator.jar(包含具体实现)


小智 7

我在 spring boot 中使用了这种验证依赖,但没有用,

<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
Run Code Online (Sandbox Code Playgroud)

我用 spring-boot-starter-validation 替换了它并且它起作用了。

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot- 
starter-validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.4.0</version>
Run Code Online (Sandbox Code Playgroud)


小智 6

如果您使用的是 spring 版本 > 3

使用 Kotlin 你应该添加 @field:Annotation

前任:

@field:NotEmpty
@field:NotNull
@field:NotBlank(message = "Email is required")
@field:Email(message = "Please provide a valid email")
val email: String,
@field:NotEmpty
@field:NotNull
@field:NotBlank(message = "Password is required")
val password: String,
Run Code Online (Sandbox Code Playgroud)

  • 有帮助,谢谢! (2认同)
  • 谢谢!我遇到了同样的问题,但是使用 Kotlin 以及您对“@field:Annotation”的评论有所帮助 (2认同)

Har*_*wal 5

我的问题就这样解决了。当我们在类中使用也需要验证的类时,因此@Valid在这种情况下需要对所有类进行注释。 链接了解更多详情