創(chuàng)建Jersey REST 服務(wù),基于Maven的實現(xiàn)
基于JavaSE形式的REST服務(wù)
創(chuàng)建項目
我們首選使用 archetypeGroupId 為 org.glassfish.jersey.archetypes 的原型,archetypeArtifactId為 jersey-quickstart-grizzly2 的原型,創(chuàng)建REST服務(wù)項目,使用IDEA創(chuàng)建項目如下:
點擊OK后,使用該原始模型創(chuàng)建項目。
運行服務(wù)
項目創(chuàng)建好后,原始模型已經(jīng)默認創(chuàng)建了一個REST服務(wù),我們可以直接啟動REST服務(wù),進入項目的根目錄,執(zhí)行如下命令構(gòu)建和啟動服務(wù):
mvnpackage
mvnexec:java
會啟動REST服務(wù),可以隨時通過回車鍵停止服務(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 可以獲取當前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ù),會輸出 Got it! 。
項目說明
啟動服務(wù)的命令 mvn exec:java,該命令實際調(diào)用了 exec-maven-plugin 插件定義的一個值為 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í)行單元測試前需要啟動服務(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)建項目
我們首選使用 archetypeGroupId 為 org.glassfish.jersey.archetypes 的原型,archetypeArtifactId為 jersey-quickstart-webapp 的原型,創(chuàng)建REST服務(wù)項目,使用 IDEA 創(chuàng)建項目如下:
運行服務(wù)
由于這個是Web項目,沒有main函數(shù),因此必須部署到Servlet容器中,才能將其運行,我們需要配置Tomcat,IDEA的配置如下:
點擊 Run菜單的 Edit Configuration,在打開的窗體中增加 Tomcat 服務(wù)配置,指定Tomcat 的安裝目錄,并設(shè)置當前站點的部署的虛擬目錄名稱,如下:
點擊OK后,就配置好Servlet容器,可以運行服務(wù)了
訪問服務(wù)
服務(wù)啟動后,我們可以訪問 http://localhost:8080/RESTWebAPP/webapi/myresource 來調(diào)用REST服務(wù),會輸出 Got it!
項目說明
Web根目錄的名稱為webapp,默認的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>
以上這篇創(chuàng)建Jersey REST 服務(wù),基于Maven的實現(xiàn)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java靜態(tài)內(nèi)部類實現(xiàn)單例過程
這篇文章主要介紹了Java靜態(tài)內(nèi)部類實現(xiàn)單例過程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10JavaWeb實現(xiàn)用戶登錄注冊功能實例代碼(基于Servlet+JSP+JavaBean模式)
這篇文章主要基于Servlet+JSP+JavaBean開發(fā)模式實現(xiàn)JavaWeb用戶登錄注冊功能實例代碼,非常實用,本文介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起看看吧2016-05-05Java?MyBatis實戰(zhàn)之QueryWrapper中and和or拼接技巧大全
在Java中QueryWrapper是MyBatis-Plus框架中的一個查詢構(gòu)造器,它提供了豐富的查詢方法,其中包括and和or方法,可以用于構(gòu)建復(fù)雜的查詢條件,這篇文章主要給大家介紹了關(guān)于Java?MyBatis實戰(zhàn)之QueryWrapper中and和or拼接技巧的相關(guān)資料,需要的朋友可以參考下2024-07-07Java版數(shù)據(jù)結(jié)構(gòu)插入數(shù)據(jù)時遇到的結(jié)點為空的問題詳解
這篇文章主要介紹了Java版數(shù)據(jù)結(jié)構(gòu)插入數(shù)據(jù)時遇到的結(jié)點為空的問題及解決辦法,需要的朋友們可以學習下。2019-09-09Java的分支結(jié)構(gòu)與循環(huán)你知道多少
這篇文章主要為大家詳細介紹了Java的分支結(jié)構(gòu)與循環(huán),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02