A simple Java REST application using Jersey, Netbeans 7 (with Maven), and Grizzly

Post to Twitter

I was reading through the documents on Jersey (for building RESTful Web services) and came across the getting started sample code. I’ve taken that code and modified it slightly to create a very simple Grizzly (embedded) REST server.


You can re-create this simple project by using Netbeans 7 and creating a new Maven Java Application called JerseyRESTGrizzly. Right-click on the dependencies and add the following artifact-ids in the query textbox that pops up:

  • jersey-server
  • grizzly-servlet-webserver
  • jersey-grizzly2

Here is my project structure (your jar versions will vary):

Create a new class file called HelloWorldResource and add the following code.

HelloWorldResource.java

package com.giantflyingsaucer.jerseyrestgrizzly;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/helloworld")
public class HelloWorldResource {

    @GET
    @Produces("text/plain")
    public String getMessage() {
        return "Hello World";
    }
}

Go into the App.java file now (this file was created by default) and add the following code:

App.java

package com.giantflyingsaucer.jerseyrestgrizzly;

import com.sun.jersey.api.container.grizzly2.GrizzlyWebContainerFactory;
import org.glassfish.grizzly.http.server.HttpServer;

import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

public class App {
    
    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost/").port(8080).build();
    }

    public static final URI BASE_URI = getBaseURI();

    protected static HttpServer startServer() throws IOException {
        final Map<String, String> initParams = new HashMap<String, String>();

        initParams.put("com.sun.jersey.config.property.packages", 
                "com.giantflyingsaucer.jerseyrestgrizzly");

        System.out.println("Starting grizzly...");
        return GrizzlyWebContainerFactory.create(BASE_URI, initParams);
    }
    
    public static void main(String[] args) throws IOException {
        HttpServer httpServer = startServer();
        System.out.println(String.format("Jersey app started with WADL available at "
                + "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...",
                BASE_URI, BASE_URI));
        System.in.read();
        httpServer.stop();
    }
}

Run the program and point your browser to this URL: http://localhost:8080/helloworld

The WADL can be found here: http://localhost:8080/application.wadl

Check out the User’s Guide for more information. Also, make sure to download all the Jersey samples and examine the sample projects they’ve created.

Post to Twitter

This entry was posted in Java, Open Source, REST. Bookmark the permalink.

2 Responses to A simple Java REST application using Jersey, Netbeans 7 (with Maven), and Grizzly

  1. Nicolás Silva says:

    Hello Chad! I have a question, Do you have examples files for this post??

    http://www.giantflyingsaucer.com/blog/?p=74

    links are broken..

    thanks

  2. Chad Lung says:

    @Nicolás,

    Nope, sorry. It looks like those files got missed when I moved my blog last time.

    Chad

Comments are closed.