I am trying to use the @Valid
and @NotBlank
annotations in my spring boot project within IntelliJ IDEA 2022.2.3 (Community Edition)
Build #IC-222.4345.14, built on October 5, 2022
I have added dependency like below in my project pom.xml
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<scope>test</scope>
</dependency>
In my entity class i have added @NotBlank
annotation as below
@NotBlank(message = "Please add the department name")
private String departmentName;
In my controller class i have added @Valid
annotation as well
@PostMapping("/departments")
public Department saveDepartment(@Valid @RequestBody Department department)
{
return departmentService.saveDepartment(department);
}
Even with all this i don't think the validation APIs are working.
My spring boot version is
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Ideally i should be getting validation error if i don't pass the department name in my POST request BUT i never get any error with blank department name where my annotations are present.
I have tried various suggested solutions like closing and restarting IntelliJ Idea, Rebuilt the project. Also, I have tried invalidating the cache. NOTHING IS WORKING !!! The dependencies also seem to be all GOOD.
What else I could try?
CodePudding user response:
Remove the scope test
from the spring-boot-starter-validation
dependency, you need this dependency during runtime as well.
Create a unit/integration-test that can test your validation. Run the test using a Maven command like mvn clean test
. If this succeeds and the same test fails when executed within IntelliJ, you have an IDE issue.
CodePudding user response:
I had to update my IntelliJ IDEA Community to latest available version which is below and everything worked good after this
IntelliJ IDEA 2022.2.4 (Community Edition)
Build #IC-222.4459.24, built on November 22, 2022
Runtime version: 17.0.5 7-b469.71 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 11 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1004M
Cores: 8
Kotlin: 222-1.7.10-release-334-IJ4459.24