Hello after spending hours reading stackoverflow answers and trying to find the answer, I'm still unable to find out why validators such as NotNull and NotBlank are not working in java. I can still send post requests with a blank name. The project is on Java 11 and Spring boot starter v2.5.3. Thank you very much in advance if you are able to figure it out, I feel I must buy you an ice cream :')
In my model:
import javax.validation.constraints.NotBlank;
@NotBlank
private final String name;
In my controller:
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
@PostMapping(path="add")
public void addUser(@Valid @NotNull @RequestBody User user) {
//Request body takes the data taken and converts it into a User
userService.addUser(user);
}
@PutMapping(path="user/{id}")
public void updateUser(@PathVariable("id") UUID id, @Valid @NotNull @RequestBody User userToUpdate) {
userService.updateUser(id, userToUpdate);
}
In my dependencies in pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
</dependencies>