創(chuàng)建Jersey REST 服務(wù),基于Maven的實(shí)現(xiàn)
基于JavaSE形式的REST服務(wù)
創(chuàng)建項(xiàng)目
我們首選使用 archetypeGroupId 為 org.glassfish.jersey.archetypes 的原型,archetypeArtifactId為 jersey-quickstart-grizzly2 的原型,創(chuàng)建REST服務(wù)項(xiàng)目,使用IDEA創(chuàng)建項(xiàng)目如下:

點(diǎn)擊OK后,使用該原始模型創(chuàng)建項(xiàng)目。
運(yùn)行服務(wù)
項(xiàng)目創(chuàng)建好后,原始模型已經(jīng)默認(rèn)創(chuàng)建了一個(gè)REST服務(wù),我們可以直接啟動(dòng)REST服務(wù),進(jìn)入項(xiàng)目的根目錄,執(zhí)行如下命令構(gòu)建和啟動(dòng)服務(wù):
mvnpackage
mvnexec:java
會(huì)啟動(dòng)REST服務(wù),可以隨時(shí)通過回車鍵停止服務(wù),輸出如下:
六月 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 可以獲取當(dāng)前REST服務(wù)公布的接口:
<resources base="http://localhost:8080/myapp/">
<resource path="myresource">
<method id="getIt" name="GET">
<response>
<representation mediaType="text/plain"/>
</response>
</method>
</resource>
</resources>
訪問服務(wù)
可以直接訪問 http://localhost:8080/myapp/myresource 就可以訪問REST服務(wù),直接訪問REST服務(wù),會(huì)輸出 Got it! 。
項(xiàng)目說明
啟動(dòng)服務(wù)的命令 mvn exec:java,該命令實(shí)際調(diào)用了 exec-maven-plugin 插件定義的一個(gè)值為 java 的 goal ,用以觸發(fā)mainClass中的main函數(shù),插件配置如下:
<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服務(wù)類為 MyResource,其 @Path 中定義了資源路徑,@GET中定義了GET方法getIt(),@Produces中定義了響應(yīng)的類型為普通字符串,示例代碼如下:
@Path("myresource")
public class MyResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getIt() {
return "Got it!";
}
}
REST服務(wù)的單元測(cè)試類MyResourceTest,在單元測(cè)試類中,在執(zhí)行單元測(cè)試前需要啟動(dòng)服務(wù),并使用Jersey Client中定義的方法來調(diào)用REST服務(wù),示例代碼如下:
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容器服務(wù)
創(chuàng)建項(xiàng)目
我們首選使用 archetypeGroupId 為 org.glassfish.jersey.archetypes 的原型,archetypeArtifactId為 jersey-quickstart-webapp 的原型,創(chuàng)建REST服務(wù)項(xiàng)目,使用 IDEA 創(chuàng)建項(xiàng)目如下:

運(yùn)行服務(wù)
由于這個(gè)是Web項(xiàng)目,沒有main函數(shù),因此必須部署到Servlet容器中,才能將其運(yùn)行,我們需要配置Tomcat,IDEA的配置如下:
點(diǎn)擊 Run菜單的 Edit Configuration,在打開的窗體中增加 Tomcat 服務(wù)配置,指定Tomcat 的安裝目錄,并設(shè)置當(dāng)前站點(diǎn)的部署的虛擬目錄名稱,如下:


點(diǎn)擊OK后,就配置好Servlet容器,可以運(yùn)行服務(wù)了
訪問服務(wù)
服務(wù)啟動(dòng)后,我們可以訪問 http://localhost:8080/RESTWebAPP/webapi/myresource 來調(diào)用REST服務(wù),會(huì)輸出 Got it!
項(xiàng)目說明
Web根目錄的名稱為webapp,默認(rèn)的Servlet容器版本為2.5,并且配置了WEB-INF/web.xml文件來配置REST服務(wù),web.xml配置如下:
<?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>
以上這篇?jiǎng)?chuàng)建Jersey REST 服務(wù),基于Maven的實(shí)現(xiàn)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java靜態(tài)內(nèi)部類實(shí)現(xiàn)單例過程
這篇文章主要介紹了Java靜態(tài)內(nèi)部類實(shí)現(xiàn)單例過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
JavaWeb實(shí)現(xiàn)用戶登錄注冊(cè)功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
這篇文章主要基于Servlet+JSP+JavaBean開發(fā)模式實(shí)現(xiàn)JavaWeb用戶登錄注冊(cè)功能實(shí)例代碼,非常實(shí)用,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-05-05
Java?MyBatis實(shí)戰(zhàn)之QueryWrapper中and和or拼接技巧大全
在Java中QueryWrapper是MyBatis-Plus框架中的一個(gè)查詢構(gòu)造器,它提供了豐富的查詢方法,其中包括and和or方法,可以用于構(gòu)建復(fù)雜的查詢條件,這篇文章主要給大家介紹了關(guān)于Java?MyBatis實(shí)戰(zhàn)之QueryWrapper中and和or拼接技巧的相關(guān)資料,需要的朋友可以參考下2024-07-07
Java版數(shù)據(jù)結(jié)構(gòu)插入數(shù)據(jù)時(shí)遇到的結(jié)點(diǎn)為空的問題詳解
這篇文章主要介紹了Java版數(shù)據(jù)結(jié)構(gòu)插入數(shù)據(jù)時(shí)遇到的結(jié)點(diǎn)為空的問題及解決辦法,需要的朋友們可以學(xué)習(xí)下。2019-09-09
基于java類路徑classpath和包的實(shí)例講解
下面小編就為大家分享一篇基于java類路徑classpath和包的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Java必備知識(shí)之位運(yùn)算及常見進(jìn)制解讀
從現(xiàn)代計(jì)算機(jī)中所有的數(shù)據(jù)二進(jìn)制的形式存儲(chǔ)在設(shè)備中。即 0、1 兩種狀態(tài),計(jì)算機(jī)對(duì)二進(jìn)制數(shù)據(jù)進(jìn)行的運(yùn)算(+、-、*、/)都是叫位運(yùn)算,即將符號(hào)位共同參與運(yùn)算的運(yùn)算2021-10-10
Java的分支結(jié)構(gòu)與循環(huán)你知道多少
這篇文章主要為大家詳細(xì)介紹了Java的分支結(jié)構(gòu)與循環(huán),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02

