Hello World with Jersey and Grizzly (from User Guide)

Tom Tresansky picture Tom Tresansky · Nov 22, 2011 · Viewed 9.9k times · Source

I'm looking at the Jersey User Guide and trying to set up a Hello World example using a Jersey web service and an embedded Grizzly server.

I'm running through Section 1 "Getting Started". I've got the code example in section 1.1 compiling just fine:

// The Java class will be hosted at the URI path "/helloworld"
 @Path("/helloworld")
 public class HelloWorldResource {

     // The Java method will process HTTP GET requests
     @GET 
     // The Java method will produce content identified by the MIME Media
     // type "text/plain"
     @Produces("text/plain")
     public String getClichedMessage() {
         // Return some cliched textual content
         return "Hello World";
     }
 }

But then I get to section 1.2, "Deploying the Root Resource", which is where I'm supposed to set up an embedded Grizzly web server to test my resource with:

public class Main {

      public static void main(String[] args) throws IOException {

          final String baseUri = "http://localhost:9998/";
          final Map<String, String> initParams = 
                           new HashMap<String, String>();

          initParams.put("com.sun.jersey.config.property.packages", 
                  "com.sun.jersey.samples.helloworld.resources");

         System.out.println("Starting grizzly...");
         SelectorThread threadSelector = 
              GrizzlyWebContainerFactory.create(baseUri, initParams);
         System.out.println(String.format(
           "Jersey app started with WADL available at %sapplication.wadl\n” + 
           “Try out %shelloworld\nHit enter to stop it...", baseUri, baseUri));
         System.in.read();
         threadSelector.stopEndpoint();
         System.exit(0);
     }    
 }

The problem is, it seems this user guide has not been updated in awhile and the class GrizzlyWebContainerFactory no longer exists!

I'm using Jersery v 1.10 and Grizzly v 1.9.41.

Can someone help me recreate this example? I know I can run the web service in a container, I'm interested in running it via the simplest possible embedded server setup that requires no additional resources (web.xml, etc.) in my project, just the 2 classes.

Answer

Tom Tresansky picture Tom Tresansky · Nov 22, 2011

I think the answer is that I need to include the jersey-grizzly dependency, then I can follow along as per the user guide.

This is NOT specified in the list of required dependencies the user guide provides:

Non-maven developers require:

grizzly-servlet-webserver.jar, jersey-server.jar, jersey-core.jar, jsr311-api.jar, asm.jar

Thanks to Ryan Stewart's answer to a similar question.