can't get started with jersey user guide

Viewed 1931

Help me please. I tried for a long time to start rest app example, but I can't do this. 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? Here is no import for this class.

3 Answers

You skipped the entire chapter 1.1. Creating a New Project from Maven Archetype which includes executing a command:

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

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

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:

<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.

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.

Related