简体   繁体   English

无法在Eclipse中创建Jersey RESTful服务

[英]Unable to create a Jersey RESTful Service in Eclipse

Im trying to create a RESTful Service on Eclipse using Jersey. 我试图使用Jersey在Eclipse上创建RESTful服务。

I repeatedly get the following error when I'm trying to run in localhost within Eclipse. 当我尝试在Eclipse中的localhost中运行时,反复出现以下错误。

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

My web.xml looks something like this : 我的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>

I have all the Jersey 2.0 jars under WEB-INF/lib as well. 我在WEB-INF / lib下也有所有Jersey 2.0罐子。 I'm running Apache Tomcat 8.0 with JDK 1.7.0_79 . 我正在使用JDK 1.7.0_79运行Apache Tomcat 8.0。

The Class used for Service entry is as follows: 用于服务输入的类如下:

@Path("/")
public class MyTestSvc {

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

        return "hello";
    }
}

Any ideas where I might be going wrong? 有什么想法我可能会出错吗?

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

if you are using maven, add the following dependency to your pom 如果您使用的是maven,请将以下依赖项添加到pom中

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

As per service class or controller your url seems to be like this 根据服务类或控制器,您的网址似乎是这样的

ProjName/rest//callService ProjName / rest // callService

So make in service class 所以在服务舱

@Path("/service")
public class MyTestSvc {

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

        return "hello";
    }
}

Then url will be ProjName/rest/service/callService 然后网址将是ProjName / rest / service / callService

There are a couple of issues in your application: 您的应用程序中存在两个问题:

Fixing the dependencies 修复依赖关系

If you are using Maven, ensure you have the following dependency: 如果使用的是Maven,请确保您具有以下依赖性:

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

If not, ensure you have all the jersey-container-servlet artifact dependencies on the classpath. 如果不是,请确保对类路径具有所有jersey-container-servlet工件依赖项。

Fixing the web.xml 修复web.xml

The jersey.config.server.provider.packages parameter defines one or more packages that contain application-specific resources and providers. jersey.config.server.provider.packages参数定义一个或多个包含特定于应用程序的资源和提供程序的软件包。 Change: 更改:

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

To: 至:

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

If you need to add more packages, use ; 如果您需要添加更多软件包,请使用; or , to separate them. ,将它们分开。

Once Tomcat 8.x supports Servlet 3.x, you don't need a web.xml deployment descriptor for simple applications. 一旦Tomcat 8.x支持Servlet 3.x,您就不需要简单应用程序的web.xml部署描述符。 For more details, check the Jersey documentation regarding deployment in Servlet 3.x containers . 有关更多详细信息,请参阅Jersey文档中有关在Servlet 3.x容器中进行部署的信息

Improving the resource path design 改善资源路径设计

Avoid @Path annotations with an empty string or just a slash as a value. 避免使用@Path注释,该注释使用空字符串或仅使用斜杠作为值。 It smells bad. 真难闻。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM