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.

[ad name=”Google Adsense”]

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.


package com.giantflyingsaucer.jerseyrestgrizzly;

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

public class HelloWorldResource {

    public String getMessage() {
        return "Hello World";

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


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


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

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


    links are broken..


  2. Chad Lung says:


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


Comments are closed.