Home > Mobile >  java springboot validate request parameters springboot @Valid annotation is invalid
java springboot validate request parameters springboot @Valid annotation is invalid

Time:04-16

Adding @NotBlank annotation to member variables in DTO does not take effect. What is the reason? Please help me

CodePudding user response:

first check this dependency is present in the pom.xml file. If it doesn't present then add. With dependency present in your project now use can use @NotBlack and @Valid annotation.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency> 
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> 
<dependency> 
    <groupId>com.h2database</groupId> 
    <artifactId>h2</artifactId>
    <version>1.4.197</version> 
    <scope>runtime</scope>
</dependency>
<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-validation</artifactId> 
</dependency>

For more information about Validation in Spring Boot check this link

CodePudding user response:

Apart from including spring-boot-starter-validation in dependency, you must include @Valid annotation on @RequestBody in the controller for @NotBlank annotation to apply on member variables in DTO .

  • Related