最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

創(chuàng)建Jersey REST 服務(wù),基于Maven的實(shí)現(xiàn)

 更新時(shí)間:2017年06月20日 09:17:17   投稿:jingxian  
下面小編就為大家?guī)硪黄獎(jiǎng)?chuàng)建Jersey REST 服務(wù),基于Maven的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

基于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)文章

  • 詳細(xì)盤點(diǎn)Java中鎖的分類

    詳細(xì)盤點(diǎn)Java中鎖的分類

    這篇文章主要介紹了詳細(xì)盤點(diǎn)Java中鎖的分類,Java中的鎖是一種多線程編程中的同步機(jī)制,用于控制線程對(duì)共享資源的訪問,防止并發(fā)訪問時(shí)的數(shù)據(jù)競(jìng)爭(zhēng)和死鎖問題,需要的朋友可以參考下
    2023-08-08
  • SpringBoot3結(jié)合Vue3實(shí)現(xià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
  • spring4新特性之web開發(fā)增強(qiáng)

    spring4新特性之web開發(fā)增強(qiáng)

    這篇文章主要介紹了spring4新特性之web開發(fā)增強(qiáng)的相關(guān)資料,需要的朋友可以參考下
    2017-09-09
  • Spring Cloud Hystrix線程池不足的解決方法

    Spring Cloud Hystrix線程池不足的解決方法

    這篇文章主要介紹了Spring Cloud Hystrix線程池不足的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 詳細(xì)聊聊JDK中的反模式接口常量

    詳細(xì)聊聊JDK中的反模式接口常量

    這篇文章主要給大家介紹了關(guān)于JDK中反模式接口常量的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用jdk具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-01-01
  • 初識(shí)Spring boot監(jiān)控

    初識(shí)Spring boot監(jiān)控

    這篇文章主要介紹了spring boot監(jiān)控的相關(guān)知識(shí),文中給大家介紹了查看監(jiān)控?cái)?shù)據(jù),數(shù)據(jù)可視化的相關(guān)知識(shí),需要的朋友可以參考下
    2018-03-03
  • Spring Boot的properties配置文件讀取

    Spring Boot的properties配置文件讀取

    這篇文章主要介紹了Spring Boot的properties配置文件讀取,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • Spring Boot LocalDateTime格式化處理的示例詳解

    Spring Boot LocalDateTime格式化處理的示例詳解

    這篇文章主要介紹了Spring Boot LocalDateTime格式化處理的示例詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-10-10
  • SpringBoot中Dozer的使用小結(jié)

    SpringBoot中Dozer的使用小結(jié)

    dozer是用來兩個(gè)對(duì)象之間屬性轉(zhuǎn)換的工具,有了這個(gè)工具之后,我們將一個(gè)對(duì)象的所有屬性值轉(zhuǎn)給另一個(gè)對(duì)象時(shí),就不需要再去寫重復(fù)的set和get方法了,下面介紹下SpringBoot中Dozer的使用,感興趣的朋友一起看看吧
    2022-03-03
  • Java簡(jiǎn)單實(shí)現(xiàn)猜數(shù)字游戲附C語(yǔ)言版本

    Java簡(jiǎn)單實(shí)現(xiàn)猜數(shù)字游戲附C語(yǔ)言版本

    猜數(shù)字是興起于英國(guó)的益智類小游戲,起源于20世紀(jì)中期,一般由兩個(gè)人或多人玩,也可以由一個(gè)人和電腦玩。游戲規(guī)則為一方出數(shù)字,一方猜,今天我們來用Java和C語(yǔ)言分別把這個(gè)小游戲?qū)懗鰜砭毦毷?/div> 2021-11-11

最新評(píng)論

湾仔区| 新源县| 四会市| 石楼县| 华阴市| 兴仁县| 鄢陵县| 武穴市| 泰来县| 勃利县| 含山县| 清河县| 青海省| 甘孜县| 青岛市| 印江| 崇信县| 文昌市| 拜泉县| 子长县| 同江市| 本溪| 富平县| 福清市| 特克斯县| 琼中| 兴文县| 离岛区| 信丰县| 稻城县| 闻喜县| 贵港市| 太保市| 乐安县| 合作市| 永康市| 普格县| 旬邑县| 五寨县| 杭州市| 海宁市|