The Java EE 6 Tutorial, Volume I

ProcedureCreating a RESTful Web Service From Maven Archetype

Although this tutorial does not present instructions on using Maven for creating applications as a general rule, because Project Jersey is built, assembled and installed using Maven, and all of its sample applications are Maven-based, this section provides an example that creates a skeleton Jersey application from a Maven archetype.

Before You Begin

This example requires that Maven be installed and configured to run from the command line on your system. Maven can be downloaded from http://maven.apache.org/.

  1. Start the Enterprise Server. For instructions on how to do this, read Starting and Stopping the Enterprise Server.

  2. After Maven is installed, run the following from the command line:

    mvn archetype:generate -DarchetypeCatalog=http://download.java.net/maven/2

    The archetype catalog will download. You will be prompted to select the type of archetype you want to create. As of the publication date of this tutorial, the following choices display in the command window. These options are likely to change, but are provided here to give you an idea of what they might look like.


    Choose archetype:
    1: http://download.java.net/maven/2/archetype-catalog.xml -> 
    jersey-quickstart-grizzly (Archetype for creating a RESTful web 
    application with Jersey and Grizzly)
    
    2: http://download.java.net/maven/2/archetype-catalog.xml -> 
    jersey-quickstart-webapp (Archetype for creating a Jersey based RESTful 
    web application with WAR packaging)
    
    3: http://download.java.net/maven/2/archetype-catalog.xml -> 
    jersey-quickstart-ejb (Archetype for creating a Jersey based RESTful EJB 
    application with WAR packaging)
    
    4: http://download.java.net/maven/2/archetype-catalog.xml -> 
    jsf2-simple-example-archetype (Simple JSF project with no non-JavaEE 
    dependencies)
  3. Select the appropriate option for the type of RESTful web service you would like to create.

    With the Grizzly-based archetype (selection 1), you will get a sample Java application, which you can run directly from Java without a need to deploy it to any container. The web application archetype (selection 2) enables you to build a WAR archive, which you could deploy onto any web Servlet container.

  4. Define a value for groupId, such as RESTHello.

  5. Define a value for artifactId, such as RESTHelloApp. This is the name of the web application as well as the directory in which the application is created.

  6. Define value for version: 1.0–SNAPSHOT. You can accept the default by not entering anything.

  7. Define value for package: groupId, such as restHello. This is the directory where the main Java files will be located, which is basedir/artifactId/src/main/java/package. If you used the example entries, this directory will be RESTHelloApp/src/main/java/restHello.

  8. Confirm properties configuration. Enter Y to confirm or N to cancel.

    Maven generates a new project containing a simple Hello World RESTful web service.

  9. Build and run your RESTful web service. First, change into the project directory, which is the artifactId, or RESTHelloApp if you used the example text.

    • For the Grizzly-based scenario (selection 1), build and run the web service on the Grizzy container using this command: mvn clean compile exec:java.

    • If you selected the WAR-based scenario (selection 2), build your WAR file using the command mvn clean package. Deploy the WAR file to your favorite Servlet container. To run it using the embedded version of GlassFish V3, use this command: mvn glassfish:run.

  10. Test the service in your browser.

    • Enter the following URL to run the Grizzly-based application (selection 1): http://localhost:9998/myresource. This is the location where it is published by default. The browser displays the text Got it!

    • Enter the following URL to run the WAR-based scenario (selection 2): http://localhost:8080/artifactId/webresources/myresource. If you used the example entries, enter http://localhost:8080/RESTHelloApp/webresources/myresource. This is the location where it is published by default. The browser displays the text Hi there!

      After starting the application using Grizzly, you should see output that looks similar to that in the example output, below:

      [INFO] ------------------------------------------------------------------------
      [INFO] Building RESTHelloApp
      [INFO]    task-segment: [clean, compile, exec:java]
      [INFO] ------------------------------------------------------------------------
      [INFO] [clean:clean]
      [INFO] [resources:resources]
      [INFO] Using default encoding to copy filtered resources.
      [INFO] [compiler:compile]
      [INFO] Compiling 2 source files to /export/home/japod/test/jaxrs-tutorial/
      			RESTHelloApp/target/classes
      [INFO] Preparing exec:java
      [INFO] No goals needed for project - skipping
      [INFO] [exec:java]
      Starting grizzly...
      Jersey app started with WADL available at http://localhost:9998/application.wadl
      Hit enter to stop it...

      After starting the application for the WAR-based scenario, you should see output that looks similar to that in the example output, below:

      [INFO] Building RESTHelloApp Jersey Webapp
      [INFO]    task-segment: [glassfish:run]
      [INFO] ------------------------------------------------------------------------
      [INFO] Preparing glassfish:run
      [INFO] [resources:resources]
      [INFO] Using default encoding to copy filtered resources.
      [INFO] snapshot org.glassfish:glassfish-parent:10.0-SNAPSHOT: checking for 
      			updates from 	glassfish-maven2-repository.dev.java.net
      [INFO] [compiler:compile]
      [INFO] Compiling 1 source file to /export/home/japod/test/jaxrs-tutorial/
      			RESTHelloApp/target/classes
      [INFO] [glassfish:run]
      Dec 8, 2009 1:20:34 AM com.sun.enterprise.v3.server.AppServerStartup run
      INFO: HK2 initialized in 479 ms
      Dec 8, 2009 1:20:34 AM com.sun.enterprise.v3.server.AppServerStartup run
      INFO: com.sun.enterprise.naming.impl.ServicesHookup@1342545 Init done in 486 ms
      Dec 8, 2009 1:20:34 AM com.sun.enterprise.v3.server.AppServerStartup run
      INFO: com.sun.enterprise.v3.server.Globals@6de609 Init done in 488 ms
      Dec 8, 2009 1:20:34 AM com.sun.enterprise.v3.server.AppServerStartup run
      INFO: com.sun.enterprise.v3.server.SystemTasks@e7e8eb Init done in 493 ms
      Dec 8, 2009 1:20:34 AM com.sun.enterprise.v3.server.AppServerStartup run
      INFO: com.sun.enterprise.v3.services.impl.HouseKeeper@1a6518 Init done in 503 ms
      Dec 8, 2009 1:20:34 AM com.sun.enterprise.v3.server.AppServerStartup run
      INFO: com.sun.enterprise.v3.services.impl.CmdLineParamProcessor@8390b0 
      			Init done in 506 ms
      JMXMP connector server URL = service:jmx:jmxmp://localhost:8888
      Dec 8, 2009 1:20:35 AM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
      INFO: Listening on port 8080
      Dec 8, 2009 1:20:35 AM com.sun.enterprise.v3.server.AppServerStartup run
      INFO: com.sun.enterprise.v3.services.impl.GrizzlyService@59cbda startup 
      			done in 815 ms
      Dec 8, 2009 1:20:35 AM com.sun.enterprise.v3.services.impl.
      			ApplicationLoaderService postConstruct
      INFO: loader service postConstruct started at 1260231635181
      Dec 8, 2009 1:20:35 AM com.sun.enterprise.v3.server.AppServerStartup run
      INFO: Application Loader startup done in 883 ms
      Dec 8, 2009 1:20:35 AM com.sun.enterprise.v3.server.AppServerStartup run
      INFO: Glassfish v3 started in 883 ms
      Dec 8, 2009 1:20:38 AM com.sun.enterprise.web.WebModuleContextConfig 
      			authenticatorConfig
      SEVERE: webModuleContextConfig.missingRealm
      Dec 8, 2009 1:20:38 AM com.sun.jersey.api.core.PackagesResourceConfig init
      INFO: Scanning for root resource and provider classes in the packages:
        restHello
      Dec 8, 2009 1:20:38 AM com.sun.jersey.api.core.PackagesResourceConfig init
      INFO: Root resource classes found:
        class restHello.MyResource
      Dec 8, 2009 1:20:38 AM com.sun.jersey.api.core.PackagesResourceConfig init
      INFO: Provider classes found:
      Dec 8, 2009 1:20:38 AM com.sun.jersey.server.impl.application.
      			WebApplicationImpl initiate
      INFO: Initiating Jersey application, version 'Jersey: 1.1.4.1 11/24/2009 01:30 AM'
      Hit ENTER for redeploy