[英]Restful WebServices in Java using Eclipse, Tomcat and Jersey
How to create simple webserver in Java using Eclipse, Tomcat and Jersey ie steps to follow?如何使用 Eclipse、Tomcat 和 Jersey 在 Java 中创建简单的网络服务器,即要遵循的步骤?
We are creating simple webserver using the below links:我们正在使用以下链接创建简单的网络服务器:
but we got an error like this:但是我们得到了这样的错误:
java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
Have maven running.有 maven 正在运行。 Then run this command(press enter if it asks sth):
然后运行此命令(如果它询问某事,请按回车键):
mvn archetype:generate -DgroupId=com.test.rest -DartifactId=test -DarchetypeArtifactId=maven-archetype-webapp
It will create you a simple webapp.它将为您创建一个简单的网络应用程序。 Now create the source package as src/main/java/com/test/rest, and create a simple class as following with a name "test" in it:
现在将源 package 创建为 src/main/java/com/test/rest,并创建一个简单的 class,其中包含名称“test”:
package com.test.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/test")
public class test{
@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {
String output = "Jersey say : " + msg;
return Response.status(200).entity(output).build();
}
}
At that point you should get errors, resolve them by adding this dependency to your pom:那时你应该得到错误,通过将此依赖项添加到你的 pom 来解决它们:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
you can run a dummy "mvn clean install" so that maven will download the repository and your errors will disappear.你可以运行一个虚拟的“mvn clean install”,这样 maven 就会下载存储库,你的错误就会消失。
Now, go to webapp/WEB-INF and configure your web.xml as follows:现在,go 到 webapp/WEB-INF 并配置你的 web.xml 如下:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</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.test.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
here we said which classes to be loaded and also gave a small prefix with "/rest".这里我们说要加载哪些类,还给了一个小前缀“/rest”。 so your webservice will start with this prefix.
所以你的网络服务将以此前缀开头。
Now you are ready, build the app, and add the jar file under tomcat/webapps folder.现在您已准备就绪,构建应用程序,并在 tomcat/webapps 文件夹下添加 jar 文件。 when you run your tomcat you can reach to your webservice via:
当您运行 tomcat 时,您可以通过以下方式访问您的网络服务:
(url_to_tomcat_server/jar_name/prefix_at_web_xml/prefix_at_java_rest_class/dummy_text_requested_byclass) (url_to_tomcat_server/jar_name/prefix_at_web_xml/prefix_at_java_rest_class/dummy_text_requested_byclass)
localhost:8080/test/rest/test/blabla
Note: tested and running注意:测试和运行
Copy all your Jersey jars, including jersey-servlet-1.12.jar, in your lib folder.将所有 Jersey jars,包括 jersey-servlet-1.12.jar 复制到您的 lib 文件夹中。 Look that you have included it in the build path.
查看您是否已将其包含在构建路径中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.