#java #mysql #hibernate #rest #tomcat10
Вопрос:
У меня есть веб-сервис проекта с гибернацией в Eclipse (Tomcat v10.0 и java JDK 8). При запуске я получил следующую ошибку: «Servlet.service() для сервлета [веб-приложение Джерси] в контексте с путем [/musichall] выдал исключение [org.glassfish.jersey.server.Исключение ContainerException: java.lang.NoClassDefFoundError: javax/проверка/ValidatorFactory] с основной причиной java.lang.Исключение ClassNotFoundException: javax.проверка.Валидаторфактория»
Ниже приведен мой pom.xml
lt;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"gt; lt;modelVersiongt;4.0.0lt;/modelVersiongt; lt;groupIdgt;org.restapi.crudlt;/groupIdgt; lt;artifactIdgt;musichalllt;/artifactIdgt; lt;packaginggt;warlt;/packaginggt; lt;versiongt;0.0.1-SNAPSHOTlt;/versiongt; lt;namegt;musichalllt;/namegt; lt;buildgt; lt;finalNamegt;musichalllt;/finalNamegt; lt;pluginsgt; lt;plugingt; lt;groupIdgt;org.apache.maven.pluginslt;/groupIdgt; lt;artifactIdgt;maven-compiler-pluginlt;/artifactIdgt; lt;versiongt;3.8.1lt;/versiongt; lt;inheritedgt;truelt;/inheritedgt; lt;configurationgt; lt;sourcegt;1.8lt;/sourcegt; lt;targetgt;1.8lt;/targetgt; lt;/configurationgt; lt;/plugingt; lt;plugingt; lt;groupIdgt;org.apache.maven.pluginslt;/groupIdgt; lt;artifactIdgt;maven-war-pluginlt;/artifactIdgt; lt;versiongt;3.3.1lt;/versiongt; lt;/plugingt; lt;/pluginsgt; lt;/buildgt; lt;dependencyManagementgt; lt;dependenciesgt; lt;dependencygt; lt;groupIdgt;org.glassfish.jerseylt;/groupIdgt; lt;artifactIdgt;jersey-bomlt;/artifactIdgt; lt;versiongt;${jersey.version}lt;/versiongt; lt;typegt;pomlt;/typegt; lt;scopegt;importlt;/scopegt; lt;/dependencygt; lt;/dependenciesgt; lt;/dependencyManagementgt; lt;dependenciesgt; lt;dependencygt; lt;groupIdgt;org.glassfish.jersey.containerslt;/groupIdgt; lt;artifactIdgt;jersey-container-servlet-corelt;/artifactIdgt; lt;!-- use the following artifactId if you don't need servlet 2.x compatibility --gt; lt;!-- artifactIdgt;jersey-container-servletlt;/artifactId --gt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;org.glassfish.jersey.injectlt;/groupIdgt; lt;artifactIdgt;jersey-hk2lt;/artifactIdgt; lt;/dependencygt; lt;!-- uncomment this to get JSON support --gt; lt;dependencygt; lt;groupIdgt;org.glassfish.jersey.medialt;/groupIdgt; lt;artifactIdgt;jersey-media-json-bindinglt;/artifactIdgt; lt;/dependencygt; lt;!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --gt; lt;dependencygt; lt;groupIdgt;org.hibernatelt;/groupIdgt; lt;artifactIdgt;hibernate-corelt;/artifactIdgt; lt;versiongt;5.6.1.Finallt;/versiongt; lt;/dependencygt; lt;!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --gt; lt;dependencygt; lt;groupIdgt;org.hibernatelt;/groupIdgt; lt;artifactIdgt;hibernate-validatorlt;/artifactIdgt; lt;versiongt;7.0.1.Finallt;/versiongt; lt;/dependencygt; lt;dependencygt; lt;groupIdgt;mysqllt;/groupIdgt; lt;artifactIdgt;mysql-connector-javalt;/artifactIdgt; lt;versiongt;8.0.27lt;/versiongt; lt;/dependencygt; lt;/dependenciesgt; lt;propertiesgt; lt;jersey.versiongt;3.0.3lt;/jersey.versiongt; lt;project.build.sourceEncodinggt;UTF-8lt;/project.build.sourceEncodinggt; lt;/propertiesgt; lt;/projectgt;
Вот мое досье «persistence.xml»
lt;?xml version="1.0" encoding="UTF-8"?gt; lt;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"gt; lt;persistence-unit name="app-DB"gt; lt;providergt;org.hibernate.jpa.HibernatePersistenceProviderlt;/providergt; lt;classgt;org.restapi.crud.musichall.model.Musicienlt;/classgt; lt;propertiesgt; lt;property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" /gt; lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3307/webservice" /gt; lt;property name="javax.persistence.jdbc.user" value="user1" /gt; lt;property name="javax.persistence.jdbc.password" value="password" /gt; lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /gt; lt;/propertiesgt; lt;/persistence-unitgt; lt;/persistencegt;
Here is the «MusicienService» 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 Listlt;Musiciengt; getAll() throws Exception { Listlt;Musiciengt; listMusicien = new ArrayListlt;gt;(); 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();} } } }
Here is the «MusicienController» 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 Listlt;Musiciengt; 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$1.run(ServerRuntime.java:263) org.glassfish.jersey.internal.Errors$1.call(Errors.java:248) org.glassfish.jersey.internal.Errors$1.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.
Большое вам спасибо!!!
Ответ №1:
javax.validation.ValidatorFactory
существует в зависимости ниже:
lt;dependencygt; lt;groupIdgt;javax.validationlt;/groupIdgt; lt;artifactIdgt;validation-apilt;/artifactIdgt; lt;versiongt;2.0.1.Finallt;/versiongt; lt;/dependencygt;
Комментарии:
1. Огромное спасибо!. теперь это работает! Я уже пытался добавить эту зависимость один раз, но она не работает. может быть, я не правильно понял версию.
2. Рад, что это работает! 🙂