读书人

Jersey创办standalone server 一

发布时间: 2012-11-13 10:00:50 作者: rapoo

Jersey创建standalone server 一

有时候,我们需要创建一个web service,但是并不需要部署在某个容器中,而是像一个应用程序一样运行,自己监听端口,解析HTTP请求,发送HTTP响应。

这样可以占用更少的资源,比如以前我碰到过一台租用的服务器由于核心线程数目的限制,Glassfish server都无法启动。现在有很多系统服务程序都是这样的实现,比如Gerrit2等。

Jersey也提供了这种方式,内部集成了grizzly2. 现在看看如何使用之。

首先用maven 的archetype从java站点上寻找合适的模板:

This XML file does not appear to have any style information associated with it. The document tree is shown below.<application xmlns="http://research.sun.com/wadl/2006/10"><doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 1.8 06/24/2011 12:17 PM"/><resources base="http://localhost:9998/"><resource path="/myresource"><method name="GET" id="getIt"><response><representation mediaType="text/plain"/></response></method></resource></resources></application>

描述了本应用程序提供了一个REST web service method,URL路径为/myresource
在浏览器上访问该地址:

http://localhost:9998/myresource

看到下面的结果。

Got it!



读书人网 >互联网

热点推荐