启动Tomcat Server

时间:2017-04-19 04:34:52

标签: java rest tomcat jersey glassfish

我正在使用Jersey和Maven在Java中构建REST API。我使用Tomcat 9作为我的服务器。一切正常,直到我今天尝试安装 RestAssured, Hamcrest, JUnit 。突然间,我的所有端点都抛出了500个内部服务器错误。 500错误的根本原因是java.lang.NoClassDefFoundError: org/glassfish/jersey/internal/inject/Binder.

我在2小时内做了什么:

  • 我试图在google上找到这个类:org/glassfish/jersey/internal/inject/Binder,但无济于事。
  • 我尝试卸载 RestAssured Hamcrest JUnit ,但它没有帮助。

这个问题让我感到沮丧。知道为什么会发生这种错误吗?提前谢谢!

以下是服务器日志的摘录:

  

严重:为servlet Jersey Web应用程序分配异常java.lang.ClassNotFoundException:org.glassfish.jersey.internal.inject.Binder       在   org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275)       在   org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1109)       在org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:178)       在org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370)       在javax.servlet.GenericServlet.init(GenericServlet.java:158)       在org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183)       在org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099)       在org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:779)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)       at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)       在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)       在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)       at org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:796)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1368)       在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)       at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)       at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.lang.Thread.run(未知来源)

下面是我的pom.xml文件:(使用RestAssured,Hamcrest和JUnit)

<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.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>2.9.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

以下是我的web.xml文件:

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.accelbyte.vincent.emailist.resources</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>

以下是我的项目结构:

Email list project structure

3 个答案:

答案 0 :(得分:8)

很可能核心Jersey和导入Binder类的Jersey扩展库之间存在版本不匹配。确保所有与Jersey相关的模块都有明确的版本(并且它们相互匹配)。或者甚至更好,通过导入jersey-bom修复您的版本。 E.g:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>2.26-b03</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

之后,您的主<debendencies>部分可能会省略版本。

答案 1 :(得分:1)

我在使用不同版本时遇到了同样的问题。确保您的Jersey-bean-validation和Jersey-container-servlet 版本相同。

<dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.22</version>
</dependency>
<!-- bean validation -->
<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-bean-validation</artifactId>
    <version>2.22</version>
</dependency>

答案 2 :(得分:0)

您遇到同样的问题。保持jersey-bean-validationjersey-container-servlet相同的版本效果很好。