RESTful WebService入门
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:\jdk16\jre\lib\alt-rt.jar
D:\jdk16\jre\lib\charsets.jar
D:\jdk16\jre\lib\deploy.jar
D:\jdk16\jre\lib\javaws.jar
D:\jdk16\jre\lib\jce.jar
D:\jdk16\jre\lib\jsse.jar
D:\jdk16\jre\lib\management-agent.jar
D:\jdk16\jre\lib\plugin.jar
D:\jdk16\jre\lib\resources.jar
D:\jdk16\jre\lib\rt.jar
D:\jdk16\jre\lib\ext\dnsns.jar
D:\jdk16\jre\lib\ext\localedata.jar
D:\jdk16\jre\lib\ext\sunjce_provider.jar
D:\jdk16\jre\lib\ext\sunmscapi.jar
D:\jdk16\jre\lib\ext\sunpkcs11.jar
D:\netwideo\restws\out\production\restws
D:\IDEA8\lib\javaee.jar
D:\netwideo\restws\lib\mail-1.4.jar
D:\netwideo\restws\lib\asm-3.1.jar
D:\netwideo\restws\lib\wadl2java.jar
D:\netwideo\restws\lib\jettison-1.0.1.jar
D:\netwideo\restws\lib\grizzly-servlet-webserver-1.8.6.4.jar
D:\netwideo\restws\lib\wadl-core.jar
D:\netwideo\restws\lib\localizer.jar
D:\netwideo\restws\lib\jdom-1.0.jar
D:\netwideo\restws\lib\jsr311-api-1.0.jar
D:\netwideo\restws\lib\stax-api-1.0-2.jar
D:\netwideo\restws\lib\persistence-api-1.0.2.jar
D:\netwideo\restws\lib\jaxb-api-2.1.jar
D:\netwideo\restws\lib\wadl-cmdline.jar
D:\netwideo\restws\lib\http-20070405.jar
D:\netwideo\restws\lib\rome-0.9.jar
D:\netwideo\restws\lib\activation-1.1.jar
D:\netwideo\restws\lib\jaxb-impl-2.1.10.jar
D:\netwideo\restws\lib\jersey-bundle-1.0.3.jar
D:\netwideo\restws\lib\jackson-lgpl-0.9.4.jar
D:\netwideo\restws\lib\FastInfoset-1.2.2.jar
D:\netwideo\restws\lib\jaxb-xjc.jar
D:\IDEA8\lib\idea_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:\jdk16\jre\lib\alt-rt.jar
D:\jdk16\jre\lib\charsets.jar
D:\jdk16\jre\lib\deploy.jar
D:\jdk16\jre\lib\javaws.jar
D:\jdk16\jre\lib\jce.jar
D:\jdk16\jre\lib\jsse.jar
D:\jdk16\jre\lib\management-agent.jar
D:\jdk16\jre\lib\plugin.jar
D:\jdk16\jre\lib\resources.jar
D:\jdk16\jre\lib\rt.jar
D:\jdk16\jre\lib\ext\dnsns.jar
D:\jdk16\jre\lib\ext\localedata.jar
D:\jdk16\jre\lib\ext\sunjce_provider.jar
D:\jdk16\jre\lib\ext\sunmscapi.jar
D:\jdk16\jre\lib\ext\sunpkcs11.jar
D:\netwideo\restws\out\production\restws
D:\IDEA8\lib\javaee.jar
D:\netwideo\restws\lib\mail-1.4.jar
D:\netwideo\restws\lib\asm-3.1.jar
D:\netwideo\restws\lib\wadl2java.jar
D:\netwideo\restws\lib\jettison-1.0.1.jar
D:\netwideo\restws\lib\grizzly-servlet-webserver-1.8.6.4.jar
D:\netwideo\restws\lib\wadl-core.jar
D:\netwideo\restws\lib\localizer.jar
D:\netwideo\restws\lib\jdom-1.0.jar
D:\netwideo\restws\lib\jsr311-api-1.0.jar
D:\netwideo\restws\lib\stax-api-1.0-2.jar
D:\netwideo\restws\lib\persistence-api-1.0.2.jar
D:\netwideo\restws\lib\jaxb-api-2.1.jar
D:\netwideo\restws\lib\wadl-cmdline.jar
D:\netwideo\restws\lib\http-20070405.jar
D:\netwideo\restws\lib\rome-0.9.jar
D:\netwideo\restws\lib\activation-1.1.jar
D:\netwideo\restws\lib\jaxb-impl-2.1.10.jar
D:\netwideo\restws\lib\jersey-bundle-1.0.3.jar
D:\netwideo\restws\lib\jackson-lgpl-0.9.4.jar
D:\netwideo\restws\lib\FastInfoset-1.2.2.jar
D:\netwideo\restws\lib\jaxb-xjc.jar
D:\IDEA8\lib\idea_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