`
阿尔萨斯
  • 浏览: 4173338 次
社区版块
存档分类
最新评论

Jersey创建standalone server 一

 
阅读更多

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

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

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

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

mvn archetype:generate -DarchetypeCatalog=http://download.java.net/maven/2
很快就看到提示选择哪种模板:

[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: http://download.java.net/maven/2 -> com.sun.jersey.archetypes:jersey-quickstart-grizzly (Archetype for creating a RESTful web application with Jersey and Grizzly)
2: http://download.java.net/maven/2 -> com.sun.jersey.archetypes:jersey-quickstart-grizzly2 (Archetype for creating a RESTful web application with Jersey and Grizzly 2.x)
3: http://download.java.net/maven/2 -> com.sun.jersey.archetypes:jersey-quickstart-webapp (Archetype for creating a Jersey based RESTful web application with WAR packaging)
4: http://download.java.net/maven/2 -> com.sun.jersey.archetypes:jersey-quickstart-ejb (Archetype for creating a Jersey based RESTful EJB application with WAR packaging)
5: http://download.java.net/maven/2 -> com.sun.faces:simple-jsf (Archetype for creating a simple JSF project)
6: http://download.java.net/maven/2 -> com.sun.faces.regression:i_jsf_XXXX-archetype (Archetype for mojarra JSF regression tests)

我选择了2.

然后填入groupId,artifactID等等信息。

Define value for property 'groupId': : com.esri
Define value for property 'artifactId': : carrier
Define value for property 'version':  1.0-SNAPSHOT: : 
Define value for property 'package':  com.esri: : 
Confirm properties configuration:
groupId: com.esri
artifactId: carrier
version: 1.0-SNAPSHOT
package: com.esri
 Y: : y

经过一段时间下载后,一切就绪。一个工程被创建出来了。

现在编译后启动它:

 mvn exec:java
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.esri:carrier:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 58, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building carrier 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> exec-maven-plugin:1.1:java (default-cli) @ carrier >>>
[INFO] 
[INFO] <<< exec-maven-plugin:1.1:java (default-cli) @ carrier <<<
[INFO] 
[INFO] --- exec-maven-plugin:1.1:java (default-cli) @ carrier ---
Starting grizzly2...
Nov 6, 2012 1:10:27 PM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [localhost:9998]
Nov 6, 2012 1:10:27 PM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
Jersey app started with WADL available at http://localhost:9998/application.wadl
Hit enter to stop it...

可以看到监听了本地的9998端口。

通过浏览器打开下面的网址:

http://localhost:9998/application.wadl

看到如下页面:

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!



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics