繁体   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