創(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ù)的單元測試類MyResourceTest,在單元測試類中,在執(zhí)行單元測試前需要啟動(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)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot3結(jié)合Vue3實(shí)現(xiàn)用戶登錄功能
最近項(xiàng)目需求搭建一個(gè)結(jié)合Vue.js前端框架和Spring Boot后端框架的登錄系統(tǒng),本文主要介紹了SpringBoot3結(jié)合Vue3實(shí)現(xiàn)用戶登錄功能,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
Spring Cloud Hystrix線程池不足的解決方法
這篇文章主要介紹了Spring Cloud Hystrix線程池不足的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Spring Boot LocalDateTime格式化處理的示例詳解
這篇文章主要介紹了Spring Boot LocalDateTime格式化處理的示例詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
Java簡單實(shí)現(xiàn)猜數(shù)字游戲附C語言版本
猜數(shù)字是興起于英國的益智類小游戲,起源于20世紀(jì)中期,一般由兩個(gè)人或多人玩,也可以由一個(gè)人和電腦玩。游戲規(guī)則為一方出數(shù)字,一方猜,今天我們來用Java和C語言分別把這個(gè)小游戲?qū)懗鰜砭毦毷?/div> 2021-11-11最新評(píng)論

