RESTful WebService入门
RESTful WebService是比基于SOAP消息的WebService简单的多的一种轻量级Web服务,RESTful WebService是没有状态的,发布和调用都非常的轻松容易。
下面写一个最简单的Hello World例子,以便对RESTful WebService有个感性认识。因为非常专业理论化的描述RESTful WebService是一件理解起来很痛苦的事情。看看例子就知道个大概了,再看理论就容易理解多了。
/**
* RESTful WebService入门
* @author leizhimin 2009-11-18 16:42:43
*/
package example;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.net.httpserver.HttpServer;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.IOException;
//指定URI
@Path("/helloworld")
public class HelloWorld {
//处理HTTP的GET请求
@GET
// 处理请求反馈的内容格式为"text/plain"
@Produces("text/plain")
public String getClichedMessage() {
return "Hello World!";
}
public static void main(String[] args) throws IOException {
//创建RESTful WebService服务
HttpServer server = HttpServerFactory.create("http://192.168.14.117:9999/");
//启动服务,这会导致新开一个线程
server.start();
//输出服务的一些提示信息到控制台
System.out.println("RESTful WebService服务已经启动");
System.out.println("服务访问地址: http://192.168.14.117:9999/helloworld");
}
}
* RESTful WebService入门
* @author leizhimin 2009-11-18 16:42:43
*/
package example;
import com.sun.jersey.api.container.httpserver.HttpServerFactory;
import com.sun.net.httpserver.HttpServer;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.io.IOException;
//指定URI
@Path("/helloworld")
public class HelloWorld {
//处理HTTP的GET请求
@GET
// 处理请求反馈的内容格式为"text/plain"
@Produces("text/plain")
public String getClichedMessage() {
return "Hello World!";
}
public static void main(String[] args) throws IOException {
//创建RESTful WebService服务
HttpServer server = HttpServerFactory.create("http://192.168.14.117:9999/");
//启动服务,这会导致新开一个线程
server.start();
//输出服务的一些提示信息到控制台
System.out.println("RESTful WebService服务已经启动");
System.out.println("服务访问地址: http://192.168.14.117:9999/helloworld");
}
}
运行这个类,控制台输入出:
2009-11-18 17:25:37 com.sun.jersey.api.core.ClasspathResourceConfig init
信息: Scanning for root resource and provider classes in the paths:
D:jdk16jrelibalt-rt.jar
D:jdk16jrelibcharsets.jar
D:jdk16jrelibdeploy.jar
D:jdk16jrelibjavaws.jar
D:jdk16jrelibjce.jar
D:jdk16jrelibjsse.jar
D:jdk16jrelibmanagement-agent.jar
D:jdk16jrelibplugin.jar
D:jdk16jrelib esources.jar
D:jdk16jrelib t.jar
D:jdk16jrelibextdnsns.jar
D:jdk16jrelibextlocaledata.jar
D:jdk16jrelibextsunjce_provider.jar
D:jdk16jrelibextsunmscapi.jar
D:jdk16jrelibextsunpkcs11.jar
D: etwideo estwsoutproduction estws
D:IDEA8libjavaee.jar
D: etwideo estwslibmail-1.4.jar
D: etwideo estwslibasm-3.1.jar
D: etwideo estwslibwadl2java.jar
D: etwideo estwslibjettison-1.0.1.jar
D: etwideo estwslibgrizzly-servlet-webserver-1.8.6.4.jar
D: etwideo estwslibwadl-core.jar
D: etwideo estwsliblocalizer.jar
D: etwideo estwslibjdom-1.0.jar
D: etwideo estwslibjsr311-api-1.0.jar
D: etwideo estwslibstax-api-1.0-2.jar
D: etwideo estwslibpersistence-api-1.0.2.jar
D: etwideo estwslibjaxb-api-2.1.jar
D: etwideo estwslibwadl-cmdline.jar
D: etwideo estwslibhttp-20070405.jar
D: etwideo estwslib ome-0.9.jar
D: etwideo estwslibactivation-1.1.jar
D: etwideo estwslibjaxb-impl-2.1.10.jar
D: etwideo estwslibjersey-bundle-1.0.3.jar
D: etwideo estwslibjackson-lgpl-0.9.4.jar
D: etwideo estwslibFastInfoset-1.2.2.jar
D: etwideo estwslibjaxb-xjc.jar
D:IDEA8libidea_rt.jar
2009-11-18 17:25:46 com.sun.jersey.api.core.ClasspathResourceConfig init
信息: Root resource classes found:
class example.HelloWorld
2009-11-18 17:25:46 com.sun.jersey.api.core.ClasspathResourceConfig init
信息: Provider classes found:
RESTful WebService服务已经启动
服务访问地址: http://192.168.14.117:9999/helloworld
信息: Scanning for root resource and provider classes in the paths:
D:jdk16jrelibalt-rt.jar
D:jdk16jrelibcharsets.jar
D:jdk16jrelibdeploy.jar
D:jdk16jrelibjavaws.jar
D:jdk16jrelibjce.jar
D:jdk16jrelibjsse.jar
D:jdk16jrelibmanagement-agent.jar
D:jdk16jrelibplugin.jar
D:jdk16jrelib esources.jar
D:jdk16jrelib t.jar
D:jdk16jrelibextdnsns.jar
D:jdk16jrelibextlocaledata.jar
D:jdk16jrelibextsunjce_provider.jar
D:jdk16jrelibextsunmscapi.jar
D:jdk16jrelibextsunpkcs11.jar
D: etwideo estwsoutproduction estws
D:IDEA8libjavaee.jar
D: etwideo estwslibmail-1.4.jar
D: etwideo estwslibasm-3.1.jar
D: etwideo estwslibwadl2java.jar
D: etwideo estwslibjettison-1.0.1.jar
D: etwideo estwslibgrizzly-servlet-webserver-1.8.6.4.jar
D: etwideo estwslibwadl-core.jar
D: etwideo estwsliblocalizer.jar
D: etwideo estwslibjdom-1.0.jar
D: etwideo estwslibjsr311-api-1.0.jar
D: etwideo estwslibstax-api-1.0-2.jar
D: etwideo estwslibpersistence-api-1.0.2.jar
D: etwideo estwslibjaxb-api-2.1.jar
D: etwideo estwslibwadl-cmdline.jar
D: etwideo estwslibhttp-20070405.jar
D: etwideo estwslib ome-0.9.jar
D: etwideo estwslibactivation-1.1.jar
D: etwideo estwslibjaxb-impl-2.1.10.jar
D: etwideo estwslibjersey-bundle-1.0.3.jar
D: etwideo estwslibjackson-lgpl-0.9.4.jar
D: etwideo estwslibFastInfoset-1.2.2.jar
D: etwideo estwslibjaxb-xjc.jar
D:IDEA8libidea_rt.jar
2009-11-18 17:25:46 com.sun.jersey.api.core.ClasspathResourceConfig init
信息: Root resource classes found:
class example.HelloWorld
2009-11-18 17:25:46 com.sun.jersey.api.core.ClasspathResourceConfig init
信息: Provider classes found:
RESTful WebService服务已经启动
服务访问地址: http://192.168.14.117:9999/helloworld
可见,服务启动成功了,下来就是从浏览器中访问服务了,输入http://192.168.14.117:9999/helloworld,将发送一个HTTP GET请求,看看结果:
测试成功了!
--------------------
这个项目以来很多的包,有些事sun的,有些不是,这里我全放出来,整个项目放到附件中,在jdk5、6下运行都可以。
很遗憾,附件最大能上传6M的东西,这个超过6M了,只能等待51cto能放宽上传文件的大小。每个限制在10M以内最好了。----希望:),这里我列出所用的包吧:
activation-1.1.jar
asm-3.1.jar
FastInfoset-1.2.2.jar
grizzly-servlet-webserver-1.8.6.4.jar
http-20070405.jar
jackson-lgpl-0.9.4.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.10.jar
jaxb-xjc.jar
jdom-1.0.jar
jersey-bundle-1.0.3.jar
jettison-1.0.1.jar
jsr311-api-1.0.jar
mail-1.4.jar
localizer.jar
persistence-api-1.0.2.jar
rome-0.9.jar
stax-api-1.0-2.jar
wadl-core.jar
wadl-cmdline.jar
wadl2java.jar
asm-3.1.jar
FastInfoset-1.2.2.jar
grizzly-servlet-webserver-1.8.6.4.jar
http-20070405.jar
jackson-lgpl-0.9.4.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.10.jar
jaxb-xjc.jar
jdom-1.0.jar
jersey-bundle-1.0.3.jar
jettison-1.0.1.jar
jsr311-api-1.0.jar
mail-1.4.jar
localizer.jar
persistence-api-1.0.2.jar
rome-0.9.jar
stax-api-1.0-2.jar
wadl-core.jar
wadl-cmdline.jar
wadl2java.jar
本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/229206