【问题标题】:Unable to create a Jersey RESTful Service in Eclipse无法在 Eclipse 中创建 Jersey RESTful 服务
【发布时间】:2017-02-22 02:26:11
【问题描述】:

我正在尝试使用 Jersey 在 Eclipse 上创建一个 RESTful 服务。

当我尝试在 Eclipse 中的 localhost 中运行时,我反复收到以下错误。

SEVERE: Servlet [Jersey Web Application] in web application [/TestPrject] threw load() exception
java.lang.ClassNotFoundException: org.glassfish.jersey.servlet.ServletContainer

我的 web.xml 看起来像这样:

<web-app 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    version="3.1">
<display-name>TestPrject</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <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.eviac.blog.MyTestSvc</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

我在 WEB-INF/lib 下也有所有 Jersey 2.0 jar。我正在使用 JDK 1.7.0_79 运行 Apache Tomcat 8.0。

Service入口使用的Class如下:

@Path("/")
public class MyTestSvc {

    @GET
    @Path("/callService")
    @Produces({ "text/plain" })
    public String userName() {

        return "hello";
    }
}

有什么我可能会出错的想法吗?

【问题讨论】:

    标签: java eclipse rest jersey tomcat8


    【解决方案1】:

    确保您有 jersey-container-servlet-core 依赖项。

    【讨论】:

      【解决方案2】:

      如果你使用 maven,在你的 pom 中添加以下依赖

      <dependency>
                  <groupId>org.glassfish.jersey.containers</groupId>
                  <artifactId>jersey-container-servlet-core</artifactId>
                  <version>{your version here (2.0)?}</version>
      </dependency>
      

      【讨论】:

      • 我没有使用 Maven
      【解决方案3】:

      根据服务类或控制器,您的网址似乎是这样的

      ProjName/rest//callService

      所以在服务类中制作

      @Path("/service")
      public class MyTestSvc {
      
          @GET
          @Path("/callService")
          @Produces({ "text/plain" })
          public String userName() {
      
              return "hello";
          }
      }
      

      那么 url 就是 ProjName/rest/service/callService

      【讨论】:

        【解决方案4】:

        您的应用程序中有几个问题:

        修复依赖关系

        如果您使用的是 Maven,请确保您具有以下依赖项:

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.23.2</version>
        </dependency>
        

        如果没有,请确保您在类路径上拥有所有 jersey-container-servlet 工件依赖项。

        修复web.xml

        jersey.config.server.provider.packages 参数定义一个或多个包,其中包含特定于应用程序的资源和提供程序。变化:

        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.eviac.blog.MyTestSvc</param-value>
        </init-param>
        

        收件人:

        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.eviac.blog</param-value>
        </init-param>
        

        如果您需要添加更多包,请使用;, 将它们分开。

        一旦 Tomcat 8.x 支持 Servlet 3.x,对于简单的应用程序,您就不需要 web.xml 部署描述符了。更多详情,请查看Jersey documentation regarding deployment in Servlet 3.x containers

        改进资源路径设计

        避免@Path 使用空字符串或仅将斜线作为值的注释。闻起来很臭。

        【讨论】:

        • 当我尝试在代码中添加ServletContainer 时,Eclipse 要求我从org.glassfish.jersey.servlet 导入,这表明添加了对jersey-container-servlet 的引用,这是需要的。但是,它在 web.xml 中我仍然面临错误。
        • 仅供参考:我没有使用 Maven
        • @RitwikSen 如果您正在为示例目的创建应用程序,那么这是开始使用 Maven 的好机会。
        猜你喜欢
        • 1970-01-01
        • 2011-02-17
        • 1970-01-01
        • 2014-05-03
        • 1970-01-01
        • 1970-01-01
        • 2017-01-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多