RESTful WebService是比基于SOAP消息的WebService简单的多的一种轻量级Web服务,RESTful WebService是没有状态的,发布和调用都非常的轻松容易。 下面写一个最简单的Hello World例子,以便对RESTful
/** * 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
可见,服务启动成功了,下来就是从浏览器中访问服务了,输入 http://192.168.14.117:9999/helloworld,将发送一个HTTP GET请求,看看结果: 测试成功了!
所用jar包
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