初始化項(xiàng)目
打開intellij idea,我的版本是version 2018.1.4。點(diǎn)擊create new project。在左側(cè)的列表中選擇maven。然后在右側(cè)勾選create from archetype。
然后在右側(cè)的列表中選擇org.apache.maven.archetypes:maven-archetype-webapp。點(diǎn)擊next。
填寫groupid和artifactid。groupid定義了項(xiàng)目屬于哪個(gè)組織,例如,我們需要使用一個(gè)包,名字叫做fastjson,用戶在項(xiàng)目中返回json數(shù)據(jù)的,是阿里的開源框架,被不少企業(yè)使用,是一個(gè)極其優(yōu)秀的json框架。它的groupid是com.alibaba,artifactid是fastjson。
簡(jiǎn)單理解一下,拿github舉個(gè)例子。groupid就相當(dāng)于是你的用戶名,而artifactid就相當(dāng)于是你的具體某個(gè)項(xiàng)目的名稱,也是我們當(dāng)前的項(xiàng)目的根目錄名稱。例子如下。
1
2
|
groupid: com.detectivehlh.test artifactid: testdemo |
點(diǎn)擊next,下兩頁(yè)不用設(shè)置,直接點(diǎn)擊next。此時(shí)新建項(xiàng)目成功,右下角會(huì)彈出一個(gè)提示框,上面寫著maven projects need to be imported.此時(shí)選擇enable auto-import。就可以看到項(xiàng)目開始自動(dòng)的去加載依賴包了。加載完成之后,項(xiàng)目會(huì)多出一個(gè)src目錄。
引入jersey和servlet
打開根目錄下pom.xml文件,在dependencies標(biāo)簽中添加如下代碼,引入servlet。
1
2
3
4
5
|
<dependency> <groupid>org.glassfish.jersey.containers</groupid> <artifactid>jersey-container-servlet</artifactid> <version> 2.22 . 2 </version> </dependency> |
打開/src/main/webapp/web_inf/web.xml。在web-app標(biāo)簽之間添加如下代碼。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<servlet> <servlet-name>jax-rs servlet</servlet-name> <servlet- class >org.glassfish.jersey.servlet.servletcontainer</servlet- class > <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.detectivehlh.test</param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name>jax-rs servlet</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping> |
新建項(xiàng)目目錄和文件
在/src/main目錄下新建java、resources目錄,java放項(xiàng)目java源代碼,resources放項(xiàng)目的靜態(tài)資源文件。
打開file中的project structure,或者使用快捷鍵,command + ;就可以快捷打開了。將剛剛創(chuàng)建的名為java目錄設(shè)置為sources,resources設(shè)置為resources。然后apply。然后在java目錄下依次新建com.detectivehlh.test三個(gè)包,就是我們的groupid.
然后在com.detectivehlh.test中新建hello類。代碼如下。
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
|
package com.detectivehlh.test; import com.alibaba.fastjson.jsonobject; import javax.ws.rs.get; import javax.ws.rs.path; import javax.ws.rs.produces; import javax.ws.rs.core.mediatype; import javax.ws.rs.core.response; import java.util.arraylist; import java.util.list; @path ( "/hello" ) public class hello { @path ( "get" ) @get @produces (mediatype.application_json) public response getstudent() { list<student> lists = new arraylist<student>(); lists.add( new student( "1" , "mayun" , 23 )); lists.add( new student( "2" , "mahuateng" , 24 )); lists.add( new student( "3" , "zhouhongyi" , 25 )); jsonobject json = new jsonobject(); return response.status(response.status.ok).entity(json.tojsonstring(lists)).build(); } } |
同樣的地方新建student類。代碼如下。
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
|
package com.detectivehlh.test; public class student { private string id; private string name; private int age; public student(string id, string name, int age) { this .id = id; this .name = name; this .age = age; } public string getid() { return id; } public void setid(string id) { this .id = id; } public string getname() { return name; } public void setname(string name) { this .name = name; } } |
引入fastjson
這個(gè)時(shí)候可以看到,hello的class中有報(bào)錯(cuò)。是因?yàn)闆]有在pom.xml中沒有引入對(duì)fastjson的依賴。在根目錄下的pom.xml中添加如下依賴。
1
2
3
4
5
|
<dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version> 1.2 . 21 </version> </dependency> |
再回到hello中就可以看到?jīng)]有錯(cuò)誤信息了。
配置tomcat
選擇頂部菜單欄中的run->edit configurations。點(diǎn)擊左側(cè)的+,選擇tomcat server->local。配置好tomcat后,選擇server旁邊的deployment標(biāo)簽,點(diǎn)擊下方的+,選擇artifact,選擇testdemo:war exploded。點(diǎn)擊apply。然后點(diǎn)擊右上角的長(zhǎng)得像播放鍵的按鈕,啟動(dòng)項(xiàng)目。
就可以看到會(huì)新建一個(gè)瀏覽器標(biāo)簽頁(yè)。顯示"hello world!",然后改變?yōu)g覽器中的路由為我們寫的接口的路由,/api/hello/get。就可以看到返回的json數(shù)據(jù)了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://segmentfault.com/a/1190000016179921