Jersey 2 + Maven + Tomcat + IntelliJ IDEA 搭建RESTful服務


  本文參考以下內容:

[1] Starting out with Jersey & Apache Tomcat using IntelliJ

[2] 【Jersey】IntelliJ IDEA + Maven + Jetty + Jersey搭建RESTful服務

   感謝兩位作者。

   網上很多文章都是用Jersey 1 搭建的,不能用Jersey 2的新特性,在此我分享一種Jersey 2的搭建方法。

0. 創建新項目

  在IntelliJ中創建新項目,選擇Java Enterprise -> RESTful Web Service -> Setup libery later.

1. 加入web框架和maven框架

  右鍵單擊項目名-> Add Frameworks Support,分別勾選Web Application和Maven。

3. 在maven中加入jersey依賴

  在pom.xml中加入:

 1 <dependency>
 2     <groupId>org.glassfish.jersey.containers</groupId>
 3     <artifactId>jersey-container-servlet</artifactId>
 4     <version>2.9</version>
 5 </dependency>
 6 <dependency>
 7     <groupId>org.glassfish.jersey.core</groupId>
 8     <artifactId>jersey-client</artifactId>
 9     <version>2.9</version>
10 </dependency>
11 <dependency>
12     <groupId>org.glassfish.jersey.media</groupId>
13     <artifactId>jersey-media-json-jackson</artifactId>
14     <version>2.9</version>
15 </dependency>

  此時,整個pom.xml文檔如下。另外使用maven默認的庫下載源文件很慢,可以使用國內鏡像,方法可見maven國內鏡像(maven下載慢的解決方法)

  順便一提,maven默認的java源值、目標值版本是1.5,可以自行修改成1.8,方法見下面代碼。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>cn.test</groupId>
 8     <artifactId>test</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10     <name>test</name>
11 
12     <properties>
13         <maven.compiler.source>1.8</maven.compiler.source>
14         <maven.compiler.target>1.8</maven.compiler.target>
15     </properties>
16 
17     <dependencies>
18         <dependency>
19             <groupId>org.glassfish.jersey.containers</groupId>
20             <artifactId>jersey-container-servlet</artifactId>
21             <version>2.9</version>
22         </dependency>
23         <dependency>
24             <groupId>org.glassfish.jersey.core</groupId>
25             <artifactId>jersey-client</artifactId>
26             <version>2.9</version>
27         </dependency>
28         <dependency>
29             <groupId>org.glassfish.jersey.media</groupId>
30             <artifactId>jersey-media-json-jackson</artifactId>
31             <version>2.9</version>
32         </dependency>
33     </dependencies>
34 </project>
View Code

4. 創建源文件

  在src/java目錄下新建包,如com.test.jersey,在包下新建類HelloWorld.java,寫上代碼:

package com.test.jersey;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/hello")
public class HelloWorld {
    //GET注解設置接受請求類型為GET
    @GET
    //Produces表明發送出去的數據類型為text/plain
    // 與Produces對應的是@Consumes,表示接受的數據類型為text/plain
    @Produces("text/plain")
    public String getMessage() {
        return "Hello world!";
    }
}

5. 配置servlet

  編輯web/WEB-INF/web.xml,加入代碼:

<servlet>
    <servlet-name>test</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.test.jersey</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

6. 配置Tomcat

  點擊Run >Edit Configurations… > “+” > Tomcat Server > Local,加入Tomcat,選擇Deployment tab, 點擊 “+”, 選擇唯一的Artifact,點擊"OK"即可。

7. 在輸出中加入庫文件

  選擇Project Structure,點擊Artifacts,可以右側Available Elements下面有很多庫文件沒有包含在輸出中。依次雙擊各個文件即可。

8. 運行Tomcat

  運行Tomcat,在瀏覽器中輸入http://localhost:8080/hello,即可看到以下輸出:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM