Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 392
Nikolas Avatar asked Dec 23 '13 13:12

Nikolas


2 Answers

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);
}
like image 145
alexey Avatar answered Oct 23 '22 13:10

alexey


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

like image 23
jzd Avatar answered Oct 23 '22 12:10

jzd