有时候,我们需要创建一个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!
分享到:
相关推荐
mule standalone server 1
mule standalone server 3
mule standalone server 2
selenium-server-standalone-2.40.0.jar 、selenium-java-2.40.0-srcs.jar 、selenium-java-2.40.0.jar
Selenium Standalone Server3.141.59 The Selenium Server is needed in order to run Remote Selenium WebDriver. Selenium 3.X is no longer capable of running Selenium RC directly, rather it does it through...
selenium-server-standalone-2.7.0.jar,适用于网络爬虫的jar包,非常好用
selenium-server-standalone-2.44.0, selenium最新服务器,
selenium3.9.0-server-standalone.jar文件,需要的赶紧下载了
selenium-server-standalone-3.9.1.jar,python插件,用于web自动化测试
从上文可以看出,Selenium Server是一个独立的中间服务器(确切地说是代理服务器),它可以架设在其他机器上!所以测试案例通过发送HTTP请求去控制Selenium Server是很正常的。 (2).Selenium Server的Launcher启动...
selenium-server-standalone-3.141.59.jar
最新版selenium-java,selenium-server-standalone-3.141.0.jar
selenium-server-standalone-3.7 selenium-server-standalone-3.7
selenium-server-standalone-4.0.0-alpha-2.zip
selenium-server-standalone-2.53.1.jar用于支持selenium webdriver的开发,也可用于分布式测试连接
selenium-server-standalone-3.6.0.jar selenium-server-standalone-3.6.0.jar
selenium-server-standalone-2.47.1.jar,2015年8月更新
selenium-server-standalone-2.14.0
selenium-server-standalone-3.141.59.jar selenium-server-standalone-3.141.59.jar
java+selenium实现web自动化测试需要用到的jar包。不过某些情况下Selenium-server-standalone包即可,Selenium-java没有使用也可以运行