java 尝试使用 Jersey 创建一个 REst 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16231926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to create a REst Service using Jersey
提问by Joel B
I am following thistutorial to create a REst Service using Jersey.
我正在按照本教程使用 Jersey 创建一个 REst 服务。
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)Created a dynamic web project and named it: de.vogella.jersey.first
1)创建一个动态的web项目并命名为:de.vogella.jersey.first
2)Installed Maven dependencies on eclipse
2)在eclipse上安装Maven依赖
3)Converted my project to a Maven project (that means created a pom.xml file)
3) 将我的项目转换为 Maven 项目(这意味着创建了一个 pom.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 :
4)添加了必要的依赖项,pom.xml
以便我可以使用 jersey 而无需手动添加 jar 文件。我添加了以下 xml :
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.17.1</version>
</dependency>
</dependencies>
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.
5)作者建议创建一个java类并给出一些代码。我只能假设他希望我们在 src 文件夹中创建一个新包,命名它de.vogella.jersey.first
然后创建一个 java 类并命名它Hello
并将代码放在那里。这就是我所做的。
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.
6)然后他建议打开web.xml
文件。虽然项目中没有这样的文件。所以我继续在WebContent/WEB-INF/lib
路径中创建这样一个文件。我放置了他建议的代码。
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:
7)接下来是我最不明白的步骤。他谈到了web.xml
我们刚刚添加的内容,更具体地说,他说:
"The parameter "com.sun.jersey.config.property.package" defines in which package jersey will look for the web service classes. This property must point to your resources classes. "
“参数“com.sun.jersey.config.property.package”定义了 jersey 将在哪个包中查找 Web 服务类。此属性必须指向您的资源类。“
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
8)最后一步是http://localhost:8080/de.vogella.jersey.first/rest/hello
在我的浏览器中打开 URL 。但是我得到HTTP Status 404 - /de.vogella.jersey.first/rest/hello
With what shall i replace exactly the com.sun.jersey.config.property.package
?
我应该用什么来替换com.sun.jersey.config.property.package
?
Are the steps that i have followed till now correct , or i misinterpreted something?
我到目前为止所遵循的步骤是正确的,还是我误解了什么?
回答by Daniel Szalay
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.
该属性com.sun.jersey.config.property.package
只需要设置为包含 Web 服务类的包。在教程中它是de.vogella.jersey.first
,您可以看到Hello
该类是在该包下声明的。
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
.
换句话说,当您部署应用程序时,Jersey 会在包中寻找 web 服务类de.vogella.jersey.first
,在这种情况下,它会找到Hello
使用javax.ws.rs.Path
注解声明的类,并创建一个 web 服务端点,监听已声明的 URL与@Path
.
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
:
但是,我从来没有为我的 Jersey 项目设置过这样的东西。我只是将我的 Web 服务类放在src
文件夹中,无论我将它们放在哪个包中,Jersey 都能识别它们。这是我在 Jersey 项目中的最低配置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>
Also if you do not fancy Maven projects, just create a simple Dynamic Web Project and copy the Jersey JARsto WebContent/WEB-INF/lib
.
此外,如果您不喜欢 Maven 项目,只需创建一个简单的动态 Web 项目并将Jersey JAR复制到WebContent/WEB-INF/lib
.
Also, as Qwerkysuggested, web.xml
has to be in WebContent/WEB-INF/
and .jar
files should be copied to WebContent/WEB-INF/lib
.
此外,正如Qwerky建议的那样,web.xml
必须在WebContent/WEB-INF/
并且.jar
文件应该复制到WebContent/WEB-INF/lib
.
Other than that, the described procedure looks fine!
除此之外,所描述的程序看起来不错!
回答by Joel B
For information if you are using Jersey 2 this class has been replaced with jersey.config.server.provider.packages
so your resource configuration would be like:
有关信息,如果您使用 Jersey 2,此类已替换为,jersey.config.server.provider.packages
因此您的资源配置将如下所示:
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>de.vogella.jersey.todo.resources</param-value>
</init-param>