can't get started with jersey user guide

1.9k views Asked by At

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

There are 3 answers

0
alexey On BEST ANSWER

Here is the link for the Main class. The Main.startServer() looks like this:

/**
 * 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);
}
0
jzd On

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.

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):

0
Marcin T.P. Łuczyński On

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.