Getting error : org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: javax/validation/ValidatorFactory

Getting error : org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: javax/validation/ValidatorFactory

我在 Eclipse 中有一个带有 Hibernate 的 Web 服务项目 (Tomcat v10.0 et java JDK 8)。 当我 运行 时,我得到了这个错误: “ Servlet.service() for servlet [Jersey Web Application] 在路径 [/musichall] 的上下文中抛出了异常 [org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: javax/validation/ValidatorFactory] 的根本原因 java.lang.ClassNotFoundException: javax.validation.ValidatorFactory"

下面是我的pom.xml

<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.restapi.crud</groupId>
    <artifactId>musichall</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>musichall</name>

    <build>
        <finalName>musichall</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <inherited>true</inherited>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
            </plugin>

        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
            <!-- artifactId>jersey-container-servlet</artifactId -->
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
        </dependency>
        <!-- uncomment this to get JSON support -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-binding</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.6.1.Final</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>7.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
        </dependency>

    </dependencies>
    <properties>
        <jersey.version>3.0.3</jersey.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>

这是我的文件“persistence.xml”

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
                   http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
             
    <persistence-unit name="app-DB">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        
      <class>org.restapi.crud.musichall.model.Musicien</class>
      
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3307/webservice" />
            <property name="javax.persistence.jdbc.user" value="user1" />
            <property name="javax.persistence.jdbc.password" value="password" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        </properties>
    </persistence-unit>
    
</persistence>

这里是“音乐服务”class


import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.restapi.crud.musichall.model.Musicien;

public class MusicienService {
    
            EntityManagerFactory entityManagerFactory = null;
            EntityManager entityManager = null;
          
            public List<Musicien> getAll() throws Exception {
                
                List<Musicien> listMusicien = new ArrayList<>();
                 try {
                        // Call persistence.xml
                        entityManagerFactory = Persistence.createEntityManagerFactory("app-DB"); 
                        entityManager = entityManagerFactory.createEntityManager();
                   
                        System.out.println("List des musiciens");
                        
                        listMusicien = entityManager.createQuery("from Musicien", Musicien.class).getResultList();
                        
                        for (Musicien musicien2 : listMusicien) {
                            System.out.println(musicien2);
                        }
                        
                        return listMusicien;
                    }
                    finally {
                    
                        if (entityManager!=null) { entityManager.close();}
                        if (entityManagerFactory!=null) {entityManagerFactory.close();}
                    }
            }
}

这里是“音乐控制器”class

package org.restapi.crud.musichall.control;

import java.util.List;

import org.restapi.crud.musichall.model.Musicien;
import org.restapi.crud.musichall.service.MusicienService;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;


@Path ("/musicien")
public class MusicienController {

        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String getIt() {
            return "Got Musicien!!!!";
        }
    
private MusicienService service = new MusicienService();

    @Path ("/getAllMusiciens")
    @GET  
    @Produces(MediaType.APPLICATION_JSON) 
    public List<Musicien> getAllMusicien () throws Exception {
        return service.getAll();
        
    }
}

错误信息

HTTP Status 500 – Internal Server Error


Type Exception Report

Message org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: javax/validation/ValidatorFactory

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception
jakarta.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: javax/validation/ValidatorFactory
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:410)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Root Cause
org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: javax/validation/ValidatorFactory
    org.glassfish.jersey.servlet.internal.ResponseWriter.rethrow(ResponseWriter.java:254)
    org.glassfish.jersey.servlet.internal.ResponseWriter.failure(ResponseWriter.java:236)
    org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:438)
    org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:263)
    org.glassfish.jersey.internal.Errors.call(Errors.java:248)
    org.glassfish.jersey.internal.Errors.call(Errors.java:244)
    org.glassfish.jersey.internal.Errors.process(Errors.java:292)
    org.glassfish.jersey.internal.Errors.process(Errors.java:274)
    org.glassfish.jersey.internal.Errors.process(Errors.java:244)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:684)
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Note The full stack trace of the root cause is available in the server logs.

非常感谢!!!

javax.validation.ValidatorFactory 存在于以下依赖项中:

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>2.0.1.Final</version>
</dependency>