如何 运行 app Hibernate 5.x, Jpa 2.1, Java EE7(javaee-api 7.0) on Tomcat7 with Jax-RS(Jersey)

How to run app Hibernate 5.x, Jpa 2.1, Java EE7(javaee-api 7.0) on Tomcat7 with Jax-RS(Jersey)

我有一个简单的应用程序 运行s 成功地作为 Java 应用程序。应用程序 Hibernate 5.x、Jpa 2.1、Java EE7(javaee-api 7.0) 中使用的框架。 然后我想添加 Jax-Rs 支持,我想 运行 在 Tomcat 7.

当我想通过 ServletContextListener 启动应用程序时遇到一些错误。似乎没有 persistence.jar WEB_INF/lib 文件夹,但我查看了 beean 创建的文件夹。

java.lang.NoClassDefFoundError: javax/persistence/Persistence
    at com.ulger.app.EntityManagerFactoryMysql.buildEntiyManagerFactory(EntityManagerFactoryMysql.java:18)
    at com.ulger.app.EntityManagerFactoryMysql.<clinit>(EntityManagerFactoryMysql.java:12)
    at com.ulger.app.Server.configure(Server.java:10)
    at com.ulger.app.Server.start(Server.java:6)
    at com.ulger.app.Initializer.contextInitialized(Initializer.java:10)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.persistence.Persistence
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    ... 14 more

tomcat目录下的应用文件夹

部署描述符

Pom.xml

    <!-- Database Connector -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.30</version>
    </dependency>
    <!-- Database Connector -->

    <!-- Javax -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
        <scope>provided</scope>
    </dependency>
    <!-- Javax -->

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.2.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>4.0.1.Final</version>
        <classifier>tests</classifier>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-envers</artifactId>
        <version>5.0.1.Final</version>
    </dependency>
    <!-- Hibernate -->

    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <version>3.2.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <!-- Logging -->

    <!-- JAX-RS (Jersey) -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.17.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.17.1</version>
    </dependency>
    <!-- JAX-RS (Jersey) -->

</dependencies>

Tomcat 7 不支持 Java EE 7,并且不会从您的 WAR 加载那些 类。如果您重新配置 tomcat 以发出调试日志输出,您应该相应地从 WAR 中看到关于 JAR 文件不是 loaded/skipped 的相应警告。

您可以尝试将您的依赖项降级到 tomcat 7 支持的版本,或者您可以尝试将 tomcat 升级到版本 8