64

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!

martins.tuga
  • 1,662
  • 1
  • 16
  • 20
  • Could you show full pom.xml plz – CodeScale May 22 '20 at 17:13
  • @CodeScale my pom is quite complex (several maven modules with lots of dependencies and plugins). I've added the section. Is there something else you are interested in? Thanks for your help. – martins.tuga May 22 '20 at 17:22
  • 1
    YOu can refer here https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/validation/annotation/Validated.html this is springs implementation, try adding @Validated to your controller – silentsudo May 22 '20 at 17:27
  • @silentsudo it was working before the migration from spring boot 2.2.5 to 2.3.0 – martins.tuga May 22 '20 at 18:08
  • @martins.tuga did you make a classpath comparison before and after ? – CodeScale May 22 '20 at 21:14

8 Answers8

117

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>
Braian Silva
  • 1,986
  • 1
  • 15
  • 21
  • @ntholi In Spring Boot version 2.3.0 need to add this dependency above to work and use Validation – Braian Silva May 27 '20 at 16:41
  • The werid thing on my screen is, that I see the `spring-boot-starter-validation` as external dependency but it does not work anyway. – E.Lmo Nov 12 '20 at 19:24
  • For me, the problem was that it's not transitive as you said. So even if it was present in a base-library it was not working on the main app and need to add it there as well. Thank you! – sebasira Jan 17 '21 at 04:15
34

According to spring boot 2.3.1 release there is no longer contains spring-boot-starter-validation with spring starter

how to add starter validation on

maven

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

Gradle

dependencies {
  ...
  implementation 'org.springframework.boot:spring-boot-starter-validation'
}

referee the release note

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#validation-starter-no-longer-included-in-web-starters

LalithK90
  • 960
  • 7
  • 15
12

If your experiencing the issue of for example: not being able to see the validation errors (default-messages) returned back to the client, this is what you could do:

Top Solution 1: Simply add devtools. This should solve the issue. After I did this, all my binding-results were returned back to the client. I recommend you to test this out first:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
</dependency>

Solution 2:

I found out that this is due to using Spring Boot 2.3+ So if youre using Spring Boot 2.3 or higher, add this dependency in your pom.xml file as its no longer included within the 'web'-dependency itself.

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

Now its necessary to set 'include binding errors' in java/resources/application.properties to "always". Same goes for 'message' as well although I think this is optional.

server.error.include-message=always
server.error.include-binding-errors=always

Solution 3: (before I discovered solution 2 which could be helpful as well)

So I found out that this is due to having Spring boot 2.3+. But I could not find caution-messages on the new updated usage of @Valid in Spring Boot v2.3+.

So I ended up switching back to Spring boot v2.2.10 (latest version of 2.2) by adjusting the release version in the pom.xml file like so:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.10.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

This worked perfectly for me by rolling back to an older version. Although id like to update my Spring Boot version some day. (Revisit solution 1 & 2)

Esther
  • 287
  • 5
  • 11
  • This answer saved the day, solution 2 worked for my issue after trying to upgrade Spring Boot from 2.2.4 to 2.4.2 – Cortex Nov 09 '21 at 21:33
  • Adding the dependency is also necessary if you didn’t do it yet because you were depending on Spring Cloud Load Balancer, as [it does not depend on it anymore](https://github.com/spring-cloud/spring-cloud-commons/issues/992) – Didier L Apr 08 '22 at 12:01
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!

martins.tuga
  • 1,662
  • 1
  • 16
  • 20
1

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>
silentsudo
  • 6,730
  • 6
  • 39
  • 81
  • thanks for your answer. it was working before the migration from spring boot 2.2.5 to 2.3.0. – martins.tuga May 22 '20 at 18:10
  • Even though, I tested with @Validated and it's not working anyway. – martins.tuga May 22 '20 at 18:13
  • @silenttudo.. seems not logic.. we talk about a minor release here so "without " breaking changes... it should work without changing any code.... – CodeScale May 22 '20 at 21:14
  • can you share any sample project as i just created this hello work sample and it started working or we may get in chat room it you still have issue making this work after upgrade – silentsudo May 23 '20 at 04:05
0

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..

0

No one actually solves the same problem. For me, I also added the dependency as instructed above. The true solution:

  1. Add the dependency
  2. mvn clean install
  3. mvn clean spring-boot:run

Everything works

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 24 '22 at 07:55
-3

A simple solution to the above issue is to add:

<repositories>
        <repository>
            <id>Central Maven repository</id>
            <name>Central Maven repository https</name>
            <url>https://repo.maven.apache.org/maven2</url>
            <layout>default</layout>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

to your POM.xml file. This will fix the issue with this version

Vipul.

  • 2
    Can you explain where the solution lies??? This is a Spring-Boot versioning vs dependency issue – Esther Nov 10 '21 at 22:54