基于JavaSE形式的REST服務
創建項目
我們首選使用 archetypeGroupId 為 org.glassfish.jersey.archetypes 的原型,archetypeArtifactId為 jersey-quickstart-grizzly2 的原型,創建REST服務項目,使用IDEA創建項目如下:
點擊OK后,使用該原始模型創建項目。
運行服務
項目創建好后,原始模型已經默認創建了一個REST服務,我們可以直接啟動REST服務,進入項目的根目錄,執行如下命令構建和啟動服務:
mvnpackage
mvnexec:java
會啟動REST服務,可以隨時通過回車鍵停止服務,輸出如下:
六月 19, 2017 11:12:23 下午 org.glassfish.grizzly.http.server.NetworkListener start
信息: Started listener bound to [localhost:8080]
六月 19, 2017 11:12:23 下午 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…
還提供了 WADL,通過訪問 application.wadl 可以獲取當前REST服務公布的接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
< resources base = "http://localhost:8080/myapp/" > < resource path = "myresource" > < method id = "getIt" name = "GET" > < response > < representation mediaType = "text/plain" /> </ response > </ method > </ resource > </ resources > |
訪問服務
可以直接訪問 http://localhost:8080/myapp/myresource 就可以訪問REST服務,直接訪問REST服務,會輸出 Got it! 。
項目說明
啟動服務的命令 mvn exec:java,該命令實際調用了 exec-maven-plugin 插件定義的一個值為 java 的 goal ,用以觸發mainClass中的main函數,插件配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
< plugin > < groupId >org.codehaus.mojo</ groupId > < artifactId >exec-maven-plugin</ artifactId > < version >1.2.1</ version > < executions > < execution > < goals > < goal >java</ goal > </ goals > </ execution > </ executions > < configuration > < mainClass >org.drsoft.rest.Main</ mainClass > </ configuration > </ plugin > |
REST服務類為 MyResource,其 @Path 中定義了資源路徑,@GET中定義了GET方法getIt(),@Produces中定義了響應的類型為普通字符串,示例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@Path("myresource") public class MyResource { @GET @Produces(MediaType.TEXT_PLAIN) public String getIt() { return "Got it!"; } } |
REST服務的單元測試類MyResourceTest,在單元測試類中,在執行單元測試前需要啟動服務,并使用Jersey Client中定義的方法來調用REST服務,示例代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
public class MyResourceTest { private HttpServer server; private WebTarget target; @Before public void setUp() throws Exception { // start the server server = Main.startServer(); // create the client Client c = ClientBuilder.newClient(); // uncomment the following line if you want to enable // support for JSON in the client (you also have to uncomment // dependency on jersey-media-json module in pom.xml and Main.startServer()) // -- // c.configuration().enable(new org.glassfish.jersey.media.json.JsonJaxbFeature()); target = c.target(Main.BASE_URI); } @After public void tearDown() throws Exception { server.stop(); } @Test public void testGetIt() { String responseMsg = target.path("myresource").request().get(String.class); assertEquals("Got it!", responseMsg); } } |
基于Servlet容器服務
創建項目
我們首選使用 archetypeGroupId 為 org.glassfish.jersey.archetypes 的原型,archetypeArtifactId為 jersey-quickstart-webapp 的原型,創建REST服務項目,使用 IDEA 創建項目如下:
運行服務
由于這個是Web項目,沒有main函數,因此必須部署到Servlet容器中,才能將其運行,我們需要配置Tomcat,IDEA的配置如下:
點擊 Run菜單的 Edit Configuration,在打開的窗體中增加 Tomcat 服務配置,指定Tomcat 的安裝目錄,并設置當前站點的部署的虛擬目錄名稱,如下:
點擊OK后,就配置好Servlet容器,可以運行服務了
訪問服務
服務啟動后,我們可以訪問 http://localhost:8080/RESTWebAPP/webapi/myresource 來調用REST服務,會輸出 Got it!
項目說明
Web根目錄的名稱為webapp,默認的Servlet容器版本為2.5,并且配置了WEB-INF/web.xml文件來配置REST服務,web.xml配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!-- This web.xml file is not required when using Servlet 3.0 container, see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html --> < web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" > < servlet > < servlet-name >Jersey Web Application</ servlet-name > < servlet-class >org.glassfish.jersey.servlet.ServletContainer</ servlet-class > < init-param > < param-name >jersey.config.server.provider.packages</ param-name > < param-value >org.drsoft.rest</ param-value > </ init-param > < load-on-startup >1</ load-on-startup > </ servlet > < servlet-mapping > < servlet-name >Jersey Web Application</ servlet-name > < url-pattern >/webapi/*</ url-pattern > </ servlet-mapping > </ web-app > |
以上這篇創建Jersey REST 服務,基于Maven的實現就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。