使用Eclipse,Tomcat和Jersey的Java中的Restful WebServices [英] Restful WebServices in Java using Eclipse, Tomcat and Jersey

查看:83
本文介绍了使用Eclipse,Tomcat和Jersey的Java中的Restful WebServices的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Eclipse,Tomcat和Jersey用Java创建简单的Web服务器,即要遵循的步骤?

How to create simple webserver in Java using Eclipse, Tomcat and Jersey i.e steps to follow?

我们正在使用以下链接创建简单的Web服务器:

We are creating simple webserver using the below links:

  • http://www.ibm.com/developerworks/web/library/wa-aj-tomcat/
  • http://www.vogella.de/articles/REST/article.html

但是我们遇到了这样的错误:

but we got an error like this:

java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

推荐答案

已使Maven运行.然后运行以下命令(如果询问是否需要按回车键):

Have maven running. Then run this command(press enter if it asks sth):

mvn archetype:generate -DgroupId=com.test.rest -DartifactId=test -DarchetypeArtifactId=maven-archetype-webapp

它将为您创建一个简单的Web应用程序.现在,将源包创建为src/main/java/com/test/rest,并创建一个简单的类,其名称如下:

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 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();

    }

}

到那时,您应该会遇到错误,请通过将此依赖项添加到pom中来解决这些错误:

At that point you should get errors, resolve them by adding this dependency to your pom:

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

您可以运行虚拟的"mvn全新安装",以便maven将下载存储库,并且错误将消失.

you can run a dummy "mvn clean install" so that maven will download the repository and your errors will disappear.

现在,转到webapp/WEB-INF并按如下所示配置您的web.xml:

Now, go to webapp/WEB-INF and configure your web.xml as follows:

<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>

在这里,我们说了要加载的类,并给出了一个小前缀"/rest".因此您的网络服务将以该前缀开头.

here we said which classes to be loaded and also gave a small prefix with "/rest". so your webservice will start with this prefix.

现在您已经准备好,构建应用程序,并将jar文件添加到tomcat/webapps文件夹下.当您运行tomcat时,您可以通过以下方式访问您的Web服务:

Now you are ready, build the app, and add the jar file under tomcat/webapps folder. when you run your tomcat you can reach to your webservice via:

(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

这篇关于使用Eclipse,Tomcat和Jersey的Java中的Restful WebServices的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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