簡體   English   中英

無法開始使用jersey用戶指南

[英]can't get started with jersey user guide

請幫幫我。 我嘗試了很長時間來啟動rest應用示例,但是我做不到。 使用澤西島用戶指南,我會陷入困境,這里是示例:

package com.example;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;

import org.glassfish.grizzly.http.server.HttpServer;

...

public class MyResourceTest {

    private HttpServer server;
    private WebTarget target;

    @Before
    public void setUp() throws Exception {
        server = Main.startServer();

        Client c = ClientBuilder.newClient();
        target = c.target(Main.BASE_URI);
    }

    @After
    public void tearDown() throws Exception {
        server.stop();
    }

    /**
     * Test to see that the message "Got it!" is sent in the response.
     */
    @Test
    public void testGetIt() {
        String responseMsg = target.path("myresource").request().get(String.class);
        assertEquals("Got it!", responseMsg);
    }
}

但我不知道,帶有startServer()方法的Main類是什么? 這里沒有此類的導入。

這是Main類的鏈接 Main.startServer()如下所示:

/**
 * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
 * @return Grizzly HTTP server.
 */
public static HttpServer startServer() {
    // create a resource config that scans for JAX-RS resources and providers
    // in $package package
    final ResourceConfig rc = new ResourceConfig().packages("$package");

    // create and start a new instance of grizzly http server
    // exposing the Jersey application at BASE_URI
    return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}

如果您在指南中閱讀了該代碼上方的段落,則說明該指南中的示例僅突出顯示部分實際代碼。 完整的代碼可在com.example包中找到,作為MyResource類。

此框架項目中生成的最后一段代碼是MyResourceTest單元測試類,與MyResource類位於同一com.example包中,但是,此單元測試類放置在maven項目測試源目錄src中。 / test / java(為簡便起見,排除了某些代碼注釋和JUnit導入):

您跳過了整章1.1。 從Maven原型創建新項目 ,包括執行命令:

mvn原型:generate -DarchetypeArtifactId = jersey-quickstart-grizzly2 \\ -DarchetypeGroupId = org.glassfish.jersey.archetypes -DinteractiveMode = false \\ -DgroupId = com.example -DartifactId =簡單服務-Dpackage = com.example \\ -DarchetypeVersion = 2.27

如果您已經有一個項目,只需在一個新的單獨目錄中運行它,等到Maven生成器完成其魔力,然后將復制依賴項復制到pom.xml中即可。

我只拿了下面兩個。 不要忘記在此處添加測試范圍標記。 結合以前添加的grizzly-http-server-jaxws依賴項,我得到的POM條目如下所示:

 <dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey</groupId> <artifactId>jersey-bom</artifactId> <version>2.27</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-grizzly2-http</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> <scope>test</scope> </dependency> </dependencies> 

並復制在src / main / java目錄樹內生成的Main.java類,該類取決於您在-Dpackage參數中的生成器中使用的值。

忽略也存在的MyResource類,如果將適當的包值放在上面的變量中,則應使用和測試您自己的REST資源API目標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM