RESTful WebService是把一切对象方法看做资源,开发RESTful WebService意味着支持在多种媒体类型以及抽象底层的客户端-服务器通信细节,如果没有一个好的工具包可用,会变得不那么容易。为了简化使用JAVA开发RESTful WebService及其客户端,一个轻量级的标准被提出:JAX-RS API。
Jersey RESTful WebService框架是一个开源的、产品级别的JAVA框架,支持JAX-RS API并且是一个JAX-RS(JSR 311和 JSR 339)的参考实现。
Jersey不仅仅是一个JAX-RS的参考实现,Jersey提供自己的API,其API继承自JAX-RS,提供更多的特性和功能以进一步简化RESTful service和客户端的开发。Jersey2.1版本之前默认HK2作为Ioc容器,后面才增加了Spring的支持。同时默认采用glassfish作为web容器。
接下里我们 使用 maven来 实践 一个 小 练习。
- 检测自己本地的maven版本:
sh-3.2# mvn -version
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T19:57:37+08:00)
Maven home: /Users/lichaoqiang/Maven/Maven3.3.3
Java version: 1.7.0_79, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.6", arch: "x86_64", family: "mac" - 创建第一个restful风格的maven工程。
在终端里面执行以下命令:mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false -DgroupId=com.licq.restful -DartifactId=licq-first-service -Dpackage=com.licq -DarchetypeVersion=2.22.1
得到的结果如下,表示创建成功。[INFO] Using following parameters for creating project from Old (1.x) Archetype: jersey-quickstart-grizzly2:2.22.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.licq.restful
[INFO] Parameter: packageName, Value: com.licq
[INFO] Parameter: package, Value: com.licq
[INFO] Parameter: artifactId, Value: licq-first-service
[INFO] Parameter: basedir, Value: /Users/lichaoqiang/project/restfulService_demo
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/lichaoqiang/project/restfulService_demo/licq-first-service
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 59.191 s
[INFO] Finished at: 2016-09-06T10:11:14+08:00
[INFO] Final Memory: 16M/245M
[INFO] ------------------------------------------------------------------------ - 使用mvn命令来编译测试新建的工程。
执行命令如下:
得到如下的结果,表示编译通过。-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.licq.MyResourceTest
Sep 06, 2016 10:17:02 AM org.glassfish.grizzly.http.server.NetworkListener start
信息: Started listener bound to [localhost:8080]
Sep 06, 2016 10:17:02 AM org.glassfish.grizzly.http.server.HttpServer start
信息: [HttpServer] Started.
Sep 06, 2016 10:17:02 AM org.glassfish.grizzly.http.server.NetworkListener shutdownNow
信息: Stopped listener bound to [localhost:8080]
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.154 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 36.341 s
[INFO] Finished at: 2016-09-06T10:17:02+08:00
[INFO] Final Memory: 16M/245M - 执行mvn exec:java命令,启动我们的服务,并查看结果。
sh-3.2# mvn exec:java
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building licq-first-service 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) > validate @ licq-first-service >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) < validate @ licq-first-service <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ licq-first-service ---
Downloading: http://192.168.29.1:8081/nexus/content/groups/public/org/apache/commons/commons-exec/1.1/commons-exec-1.1.jar
Downloading: http://192.168.29.1:8081/nexus/content/groups/public/org/codehaus/plexus/plexus-container-default/1.0-alpha-9/plexus-container-default-1.0-alpha-9.jar
Downloaded: http://192.168.29.1:8081/nexus/content/groups/public/org/apache/commons/commons-exec/1.1/commons-exec-1.1.jar (52 KB at 30.3 KB/sec)
Downloaded: http://192.168.29.1:8081/nexus/content/groups/public/org/codehaus/plexus/plexus-container-default/1.0-alpha-9/plexus-container-default-1.0-alpha-9.jar (191 KB at 93.7 KB/sec)
Sep 06, 2016 10:18:08 AM org.glassfish.grizzly.http.server.NetworkListener start
信息: Started listener bound to [localhost:8080]
Sep 06, 2016 10:18:08 AM org.glassfish.grizzly.http.server.HttpServer start
信息: [HttpServer] Started.
Jersey app started with WADL available at http://localhost:8080/myapp/application.wadl
Hit enter to stop it...
在浏览器中输入地址:http://localhost:8080/myapp/application.wadl查看wsdl的描述。
在浏览器中输入地址:http://localhost:8080/myapp/myresource查看我们的资源,也就是方法。
这个小实践的源码在Github上有备份,可以下载查看,很简单,后面会有更多的详解。