天天看點

使用IntelliJ IDEA建立Java Web後端resfulAPI模闆

初始化項目

打開IntelliJ IDEA,我的版本是Version 2018.1.4。點選Create New Project。在左側的清單中選擇Maven。然後在右側勾選Create from archetype。

然後在右側的清單中選擇org.apache.maven.archetypes:maven-archetype-webapp。點選next。

填寫GroupId和ArtifactId。GroupId定義了項目屬于哪個組織,例如,我們需要使用一個包,名字叫做fastjson,使用者在項目中傳回json資料的,是阿裡的開源架構,被不少企業使用,是一個極其優秀的Json架構。它的groupId是com.alibaba,artifactId是fastjson。

簡單了解一下,拿Github舉個例子。GroupId就相當于是你的使用者名,而ArtifactId就相當于是你的具體某個項目的名稱,也是我們目前的項目的根目錄名稱。例子如下。

GroupId: com.detectivehlh.test
ArtifactId: testDemo           

點選next,下兩頁不用設定,直接點選next。此時建立項目成功,右下角會彈出一個提示框,上面寫着Maven projects need to be imported.此時選擇Enable Auto-Import。就可以看到項目開始自動的去加載依賴包了。加載完成之後,項目會多出一個src目錄。

引入jersey和servlet

打開根目錄下pom.xml檔案,在dependencies标簽中添加如下代碼,引入servlet。

<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标簽之間添加如下代碼。

<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>           

建立項目目錄和檔案

在/src/main目錄下建立java、resources目錄,java放項目java源代碼,resources放項目的靜态資源檔案。

打開File中的Project Structure,或者使用快捷鍵,command + ;就可以快捷打開了。将剛剛建立的名為java目錄設定為Sources,resources設定為Resources。然後Apply。然後在java目錄下依次建立com.detectivehlh.test三個包,就是我們的GroupId.

然後在com.detectivehlh.test中建立Hello類。代碼如下。

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類。代碼如下。

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

這個時候可以看到,Hello的class中有報錯。是因為沒有在pom.xml中沒有引入對fastjson的依賴。在根目錄下的pom.xml中添加如下依賴。

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.21</version>
</dependency>           

再回到Hello中就可以看到沒有錯誤資訊了。

配置Tomcat

選擇頂部菜單欄中的Run->Edit Configurations。點選左側的+,選擇Tomcat Server->local。配置好Tomcat後,選擇Server旁邊的Deployment标簽,點選下方的+,選擇Artifact,選擇testDemo:war exploded。點選Apply。然後點選右上角的長得像播放鍵的按鈕,啟動項目。

就可以看到會建立一個浏覽器标簽頁。顯示"Hello World!",然後改變浏覽器中的路由為我們寫的接口的路由,/api/hello/get。就可以看到傳回的json資料了。