RESTful Web Services Developer's Guide

Using NetBeans to Create Jersey Applications

This section gives a simple introduction to using Jersey in NetBeans.

ProcedureCreating a Jersey-Annotated Web Application using NetBeans IDE

This section describes, using a very simple example, how to create a Jersey-annotated web application.

Before You Begin

Before you can deploy a Jersey application using NetBeans, you must have installed the RESTful Web Services plugin, as described in Installing Jersey in NetBeans.

  1. In NetBeans IDE, create a simple web application. For this example, we will work with a very simple “Hello, World” web application.

    1. Open NetBeans IDE.

    2. Select File->New Project.

    3. From Categories, select Java Web. From Projects, select Web Application. Click Next.

    4. Enter a project name, HelloWorldApp, click Next.

    5. Make sure the Server is GlassFish v3 Prelude.

    6. Click Finish.

  2. The project will be created. The file index.jsp will display in the Source pane.

  3. Right-click the project and select New, then select RESTful Web Services from Patterns.

    1. Select Singleton to use as a design pattern. Click Next.

    2. Enter a Resource Package name, like HelloWorldResource. For MIME Type select text/html.

    3. Enter /helloworld in the Path field. Enter HelloWorld in the Resource Name field.

    4. Click Finish.

      A new resource, HelloWorldResource.java, is added to the project and displays in the Source pane.

  4. In HelloWorldResource.java, modify or add code to resemble the following example.

    /**
         * Retrieves representation of an instance of helloworld.HellowWorldResource
         * @return an instance of java.lang.String
         */
        @GET
        @Produces("text/html")
        public String getXml() {
            return "<html><body><h1>Hello World!</body></h1></html>";
        }
    
        /**
         * PUT method for updating or creating an instance of HelloWorldResource
         * @param content representation for the resource
         * @return an HTTP response with content of the updated or created resource.
         */
        @PUT
        @Consumes("application/xml")
        public void putXml(String content) {
        } 

ProcedureDeploying and Testing a Jersey Application using NetBeans IDE

This section describes building, deploying, and testing a Jersey-annotated application using NetBeans IDE.

  1. Right-click the project node and click Test RESTful Web Services.

    This step will deploy the application and bring up a test client in the browser.

  2. When the test client displays, navigate to the helloworld resource and click the Test button.

    The words Hello World! will display in the Response window.

See Also

For other sample applications that demonstrate deploying and running Jersey applications using NetBeans, read Building and Running the HelloWorld-WebApp Application in NetBeans, Building and Running the Bookstore Application from NetBeans IDE, or look at the tutorials on the NetBeans tutorial site, such as the one titled Getting Started with RESTful Web Services: NetBeans 6.5. This tutorial includes a section on creating a CRUD application from a database.