Getting javax.validation.NoProviderFoundException exception even after adding the dependencies
Asked Answered
S

2

12

I am new to Java and maven. I might be missing something obvious, but I am not able to figure it out.

I was trying to add javax validations to my project following the link : https://www.baeldung.com/javax-validation

And I have added the following dependencies

      <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.1.Final</version>
      </dependency>
      <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.13.Final</version>
      </dependency>
      <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.el</artifactId>
        <version>3.0.0</version>
      </dependency>

I have tried group ids org.hibernate and org.hibernate.validator also.

But I was getting a runtime error during the execution of buildDefaultValidatorFactory() of:

import javax.validation.Validation;
import javax.validation.Validator;
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

I was getting the following error

Method threw 'javax.validation.NoProviderFoundException' exception.
Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.

What am I missing? Let me know if you need some key info. I have been fighting with this for hours. So, thanks in advance.

Swaziland answered 29/11, 2020 at 15:16 Comment(0)
C
0

Remove the hibernate and glassfish validation. Just keep javax one. Clean your project properly.

Then right do project->runas->maven build ->clean install once.

At the same time you can update your project also click on project ->maven>update project

Chinfest answered 29/11, 2020 at 15:26 Comment(1)
I just had javax only. But when I had to use the validator(Validator validator = Validation.buildDefaultValidatorFactory().getValidator();). I added hibernate(following #36329501). But still getting the error.Swaziland
C
0

Your Question is unclear about your app. Are you building a web app or a desktop app (console or GUI)? Let's cover both.

Instructions here are for a Java project using the Apache Maven build system. But you can easily adjust for Gradle.

Jakarta EE

And let's update for the soon-to-be current Jakarta EE 11. FYI, a few years ago, Oracle donated their Java EE technologies to the Eclipse Foundation where it was re-invented as Jakarta EE. After the first edition of Jakarta EE, the package names were changed from javax.* to jakarta.*.

Let's use Jakarta Validation 3.1, part of Jakarta EE 11.

Interface versus Implementation

The idea of Jakarta EE is to specify a standard set of interfaces that define a particular enterprise-oriented technology such as Jakarta Validation. Then any number of vendors can choose to build a product that implements those interfaces.

Interface

For Jakarta EE 11 generation of technologies, use the Jakarta Validation 3.1 specification.

<!-- https://mvnrepository.com/artifact/jakarta.validation/jakarta.validation-api -->
<dependency>
    <groupId>jakarta.validation</groupId>
    <artifactId>jakarta.validation-api</artifactId>
    <version>3.1.0</version>
    <scope>…</scope>
</dependency>

Implementation

Currently, the only implementation of Jakarta Validation 3.1 is Hibernate Validator version 9. See their Releases page for more info.

<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>9.0.0.Beta2</version>
    <scope>…</scope>
</dependency>

If you write your app in such a way as to program to the interface rather than program to the implementation, then you are free to swap out one implementation for another. This avoids vendor lock-in.

Scope

Notice in the XML snippets for the dependency elements below that we have incomplete scope elements nested. We will complete those further down.

Learn more about scope in the Depndency Scope section of the Maven manual.

Scope values include:

  • compile
    (default) Used for compilation and included in final artifact.
  • provided
    Used for compilation, but not included in final artifact. Expected to be already on the classpath in deployment, such as with a Jakarta EE server.
  • runtime
    Not used in compilation, but is included in final artifact.
  • test
    Used only for testing compilation and for execution of tests. Not used in regular app compilation & execution. Mostly for testing libraries such as JUnit & Mockito.
  • system
    Indicates you want to specify the location of a copy of the desired library already found on your local machine rather that have Maven download from a Maven artifact repository.
  • import
    Read a list of dependencies from another POM file.

Jakarta Expression Language

A further detail: Hibernate Validator requires an implementation of another Jakarta EE technology, Jakarta Expression Language (formerly known as EL) (see Wikipedia). See the Hibernate Validator manual, section 1.1.1. Unified EL.

Our example here uses Jakarta EE 11, so we need version 6.0 of Jakarta Expression Language, with 6.0.1 being the latest update.

If curious, here is the Maven coordinates for the interfaces. We need not include this as our implementation (tomcat-jasper-el) brings its own copy.

<!-- https://mvnrepository.com/artifact/jakarta.el/jakarta.el-api -->
<dependency>
    <groupId>jakarta.el</groupId>
    <artifactId>jakarta.el-api</artifactId>
    <version>6.0.1</version>
    <scope>…</scope>
</dependency>

I found one implementation from the Apache Tomcat project. Other implementations will appear when Jakarta EE 11 arrives. This implementation tomcat-jasper-el happens to bring with it a copy of the API in a library named tomcat-el-api.

<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper-el -->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper-el</artifactId>
    <version>11.0.0-M24</version>
</dependency>

Desktop app

For a console or GUI app (JavaFX, Swing, SWT) you must include both the interfaces and the implementation into your final software artifact (such as a JAR file). As discussed above, by including both you can program to the interface rather than the implementation.

So the dependencies subsection of your Maven POM would look like this:

<dependencies>

    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.11.0</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/jakarta.validation/jakarta.validation-api -->
    <dependency>
        <groupId>jakarta.validation</groupId>
        <artifactId>jakarta.validation-api</artifactId>
        <version>3.1.0</version>
        <scope>compile</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>9.0.0.Beta2</version>
        <scope>compile</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper-el -->
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper-el</artifactId>
        <version>11.0.0-M24</version>
        <scope>compile</scope>
    </dependency>

</dependencies>

Note that in both the jakarta.validation-api & hibernate-validator elements, we specified of a scope of compile to use a downloaded copy of the libraries for both compilation and for inclusion in our app’s final artifact (a JAR file). In contrast we marked the junit-jupiter-api dependency with a scope of test, because we use that only during testing but not in deployment.

With those dependencies, our desktop app will successfully run the Jakarta Validation code seen in the Question.

package work.basil.example;

import jakarta.validation.Validation;
import jakarta.validation.Validator;

public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        System.out.println( "validator = " + validator );
    }
}

Alternatively, you may choose to skip the Jakarta EE interfaces, and just program to the implementation. This gives you access to possibly more features, but locks you in to that particular implementation product. To make this choice, omit the jakarta.validation-api element above, and write your code using the packages found within the Hibernate Validator API rather than the jakarta.* packages.

FYI, here is a complete POM file used to build a simple console app demo. This POM was created by a Maven archetype named QuickStart, but with all its dependencies updated.

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>work.basil.example</groupId>
    <artifactId>ExValid</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>ExValid</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>22</maven.compiler.release>
    </properties>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.11.0</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/jakarta.validation/jakarta.validation-api -->
        <dependency>
            <groupId>jakarta.validation</groupId>
            <artifactId>jakarta.validation-api</artifactId>
            <version>3.1.0</version>
            <scope>compile</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>9.0.0.Beta2</version>
            <scope>compile</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper-el -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper-el</artifactId>
            <version>11.0.0-M24</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.4.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.3.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.13.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>3.3.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.4.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>3.1.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>3.1.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.12.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.6.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Web app

In a Web app, you will be deploying to a Jakarta EE compliant server that supports either the Web profile or the “full” Platform profile of the Jakarta EE. Both of these profiles require an implementing server product to support Jakarta Validation.

This means that at runtime, your web app can count on both the Jakarta Validation interfaces and an implementation to be present.

So, during development we need a copy of the interfaces only for compilation but not for inclusion in our final artifact (likely a WAR or EAR file). So we can include only the interfaces, with a scope of provided. No implementation needed.

For a web app, if deploying to a server that supports either the Web or Full profile of Jakarta EE 11, then we only specify a dependency for that profile. Both profiles include the interfaces for Jakarta Validation. And any implementation of either profile will include both the interfaces and an implementation. So all we need here is either profile with a scope of provided.

<dependencies>

    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.11.0</version>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/jakarta.platform/jakarta.jakartaee-web-api -->
    <dependency>
        <groupId>jakarta.platform</groupId>
        <artifactId>jakarta.jakartaee-web-api</artifactId>
        <version>11.0.0-M4</version>
        <scope>provided</scope>
    </dependency>

</dependencies>

Tomcat or Jetty

If you are using a Web container that does not fully support either the Web or Full profile of Jakarta EE, such as Apache Tomcat or Eclipse Jetty, then you are in a situation similar to the desktop app development discussed above.

Neither Tomcat nor Jetty include the Jakarta Validation interfaces nor an implementation. So you must include both. See the dependencies XML snippet seen in the desktop app discussion above.

Cigarette answered 2/9, 2024 at 5:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.