问题
I've migrated a Spring Boot project from 2.2.5 to 2.3.0 and after that, Validations stopped to work (they aren't invoked at all).
I read in changelog documentation (https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3.0-M1-Release-Notes), that spring-boot-starter-validation
now needs to be added manually as a dependency.
So, I added it to my pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
My pom parent is:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath></relativePath>
</parent>
My Controller looks like this:
@PostMapping( value = "/signup", consumes = MediaType.APPLICATION_JSON_VALUE )
@ResponseStatus( value = HttpStatus.OK )
public void signUp( @Valid @RequestBody ClientDto clientDto )
{
onboardingService.signUp( clientDto );
}
EDIT:
I WAS ABLE TO FOUND THE ISSUE, CHECK MY ANSWER BELOW!
Thanks everybody for the help!
回答1:
Validation starter not included in web starters anymore.
The spring-boot-starter-validation is not a transitive dependency of spring-boot-starter-web and spring-boot-starter-webflux anymore.
Add this dependency for validations work.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
回答2:
Actually there error was in the unit tests. The validation was working well.
For those who came here with the same issue, it's very likely that you are missing to add the following dependency to the pom.xml as Braian Silva suggested above:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Thanks for your help guys!
回答3:
You need to validate the validations of ClientDto
by using @Valid
. This is how I validate, and working fine
@PostMapping( value = "/signup", consumes = MediaType.APPLICATION_JSON_VALUE )
@ResponseStatus( value = HttpStatus.OK )
public void signUp( @Valid @RequestBody ClientDto clientDto, BindingResult bindingResult, )
{
// If any hibernate errors
if (bindingResult.hasErrors()) {
Set<String> errors = new HashSet<>();
for (ObjectError res : bindingResult.getFieldErrors()) {
errors.add(res.getDefaultMessage());
}
throw new RuntimeException (errors.toString());
}
onboardingService.signUp( clientDto );
}
回答4:
Hi You have to annotate you controller class with @Validated
annotation see example below:
For testing purpose please try commenting @Validated
annotation you won't notice javax.validation.ConstraintViolationException: hello.name: size must be between 4 and 10
but once you place it back its works again.
More technical info here Difference between @Valid and @Validated in Spring
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Validated
@RestController
@RequestMapping("/hello")
class HelloController {
@GetMapping
public String hello(@Valid
@NotNull(message = "Name cannot be empty")
@Size(min = 4, max = 10) @RequestParam("name") String name) {
return "Hello, " + name + "!";
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
回答5:
I've got a similar issue here. But in my case, the validation is warning me that a sequence is missed in oracle schema, but it is there. I think that is a bug.. I will follow with the 2.4.0 version for while..
来源:https://stackoverflow.com/questions/61959918/spring-boot-validations-stopped-working-after-upgrade-from-2-2-5-to-2-3-0