尝试使用Jersey创建REst服务 [英] Trying to create a REst Service using Jersey

查看:97
本文介绍了尝试使用Jersey创建REst服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注这个教程,使用Jersey创建一个REst服务。

I am following this tutorial to create a REst Service using Jersey.

有时我无法完全理解教程作者的意思,但这些是我到目前为止所遵循的步骤:

Sometimes i fail to understand fully what the author of the tutorial means but these are the steps that i have followed so far :

1) 创建了一个动态网络项目,并将其命名为 de.vogella.jersey.first

2) 在eclipse上安装Maven依赖项

3) 将我的项目转换为Maven项目(这意味着创建了一个pom.xml文件)

4) pom.xml 中添加了必要的依赖项,这样我就可以使用jersey而无需手动添加jar文件。我添加了以下xml:

4) Added the necessary dependencies in pom.xml so that i can use jersey without having to manually add the jar files. I added the following xml :

<dependencies>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.17.1</version>
    </dependency>
</dependencies>

5) 作者建议创建一个java类,给出一些代码。我只能假设他希望我们在src文件夹中创建一个新包,将其命名为 de.vogella.jersey.first 然后创建一个java类并将其命名为 Hello 并将代码放在那里。这就是我做的。

5) The author suggests to create a java class and gives some code. I can only assume that he wants us to create a new package in the src folder , name it de.vogella.jersey.first and then create a java class and name it Hello and place the code there. Thats what i did.

6) 然后他建议打开网页。 xml 文件。虽然在项目中不是这样的文件。所以我继续在 WebContent / WEB-INF / lib 路径中创建这样一个文件。我放置了他建议的代码。

6) Then he suggests to open the web.xml file. Theres not such a file in the project though. So i go ahead and create such a file in the WebContent/WEB-INF/lib path. I place the code that he suggest.

7) 接下来是我最不了解的步骤。他谈到我们刚刚添加的 web.xml ,更具体地说他说:

7) Next is the step that i fail to understand most. He talks about the web.xml that we just added and more specifically he states:

参数com.sun.jersey.config.property.package定义了jersey将在哪个包中查找Web服务类。此属性必须指向您的资源类。

8) 最后一步是打开网址 http:// localhost:8080 / de.vogella.jersey.first /在我的浏览器中休息/你好。但是我得到 HTTP状态404 - /de.vogella.jersey.first/rest/hello

8) Last step is open the URL http://localhost:8080/de.vogella.jersey.first/rest/hello in my browser. However i get HTTP Status 404 - /de.vogella.jersey.first/rest/hello

我应该如何更换 com.sun.jersey.config.property.package

我现在遵循的步骤是正确的,还是我误解了什么?

Are the steps that i have followed till now correct , or i misinterpreted something?

推荐答案

属性 com.sun.jersey.config.property.package 只需要设置为包含Web服务类的包。在本教程中,它是 de.vogella.jersey.first ,你可以看到 Hello 类是在该包。

The property com.sun.jersey.config.property.package just needs to be set as the package that contains the web service classes. In the tutorial it is de.vogella.jersey.first, and you can see that the Hello class is declared under that package.

换句话说,当您部署应用程序时,Jersey将在 de.vogella.jersey包中查找Web服务类。第一个,在这种情况下,它将找到使用 javax.ws.rs.Path <声明的类 Hello / code>注释,并创建一个Web服务端点,侦听已使用 @Path 声明的URL。

In other words, when you deploy the application, Jersey will look for web service classes in the package de.vogella.jersey.first, and in this case it will find the class Hello being declared with the javax.ws.rs.Path annotation, and create a web service endpoint listening on the URL that has been declared with @Path.

但是,我从来没有为我的泽西项目设置过这样的东西。我只是将我的Web服务类放在 src 文件夹中,无论我把它放在哪个包中,Jersey都会识别它们。这是泽西项目在 web.xml 中的最低配置:

However, I have never set such a thing for my Jersey projects. I just put my web service classes in the src folder, and Jersey recognizes them no matter which package I put them inside. This is the minimum configuration that I have with Jersey projects in web.xml:

<servlet>
    <description>JAX-RS Tools Generated - Do not modify</description>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>

    <!-- 
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.your.webservice.classes</param-value>
    </init-param> 
    -->

    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

此外,如果您不喜欢Maven项目,只需创建一个简单的动态Web项目并复制 Jersey JAR WebContent / WEB-INF / lib

Also if you do not fancy Maven projects, just create a simple Dynamic Web Project and copy the Jersey JARs to WebContent/WEB-INF/lib.

另外, Qwerky 建议, web.xml 必须在 WebContent / WEB-INF / .jar 文件应复制到 WebContent / WEB-INF / lib

Also, as Qwerky suggested, web.xml has to be in WebContent/WEB-INF/ and .jar files should be copied to WebContent/WEB-INF/lib.

除此之外,描述的程序看起来很好!

Other than that, the described procedure looks fine!

这篇关于尝试使用Jersey创建REst服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆