简体   繁体   English

无法开始使用jersey用户指南

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

Help me please. 请帮帮我。 I tried for a long time to start rest app example, but I can't do this. 我尝试了很长时间来启动rest应用示例,但是我做不到。 Using jersey user guide I'm get stuck with it.Here is example: 使用泽西岛用户指南,我会陷入困境,这里是示例:

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);
    }
}

but i can't realize, what is the Main class with the startServer() method? 但我不知道,带有startServer()方法的Main类是什么? Here is no import for this class. 这里没有此类的导入。

Here is the link for the Main class. 这是Main类的链接 The Main.startServer() looks like this: 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);
}

If you read the paragraph above this code in the guide, it explains that the example in the guide highlights only part of the real code. 如果您在指南中阅读了该代码上方的段落,则说明该指南中的示例仅突出显示部分实际代码。 The complete code is found in the com.example package as the MyResource class. 完整的代码可在com.example包中找到,作为MyResource类。

The last piece of code that has been generated in this skeleton project is a MyResourceTest unit test class that is located in the same com.example package as the MyResource class, however, this unit test class is placed into the maven project test source directory src/test/java (certain code comments and JUnit imports have been excluded for brevity): 此框架项目中生成的最后一段代码是MyResourceTest单元测试类,与MyResource类位于同一com.example包中,但是,此单元测试类放置在maven项目测试源目录src中。 / test / java(为简便起见,排除了某些代码注释和JUnit导入):

You skipped the entire chapter 1.1. 您跳过了整章1.1。 Creating a New Project from Maven Archetype which includes executing a command: 从Maven原型创建新项目 ,包括执行命令:

mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \\ -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \\ -DgroupId=com.example -DartifactId=simple-service -Dpackage =com.example \\ -DarchetypeVersion=2.27 mvn原型:generate -DarchetypeArtifactId = jersey-quickstart-grizzly2 \\ -DarchetypeGroupId = org.glassfish.jersey.archetypes -DinteractiveMode = false \\ -DgroupId = com.example -DartifactId =简单服务-Dpackage = com.example \\ -DarchetypeVersion = 2.27

If you already have a project, just run it in a new, separate directory, wait until Maven generator finishes its magic and then copy copy dependencies put into pom.xml 如果您已经有一个项目,只需在一个新的单独目录中运行它,等到Maven生成器完成其魔力,然后将复制依赖项复制到pom.xml中即可。

I took only those two below. 我只拿了下面两个。 Dont' forget about adding test scope markers there. 不要忘记在此处添加测试范围标记。 Combined with previously added grizzly-http-server-jaxws dependency, my resulting POM entries look as follow: 结合以前添加的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> 

and copy Main.java class which is generated inside src/main/java directories tree which depends on the values you used in the generator in -Dpackage parameter. 并复制在src / main / java目录树内生成的Main.java类,该类取决于您在-Dpackage参数中的生成器中使用的值。

Ignore MyResource class that's also there, if you put proper package value in the variable above, your own REST resource API targets should be used and tested. 忽略也存在的MyResource类,如果将适当的包值放在上面的变量中,则应使用和测试您自己的REST资源API目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM