The Java EE 6 Tutorial

A RESTful Web Service

This section explains how to use NetBeans IDE to create a RESTful web service. NetBeans IDE generates a skeleton for the application, and you simply need to implement the appropriate methods. If you do not use an IDE, try using one of the example applications that ship with Jersey as a template to modify.

ProcedureTo Create a RESTful Web Service Using NetBeans IDE

  1. In NetBeans IDE, create a simple web application. This example creates a very simple “Hello, World” web application.

    1. In NetBeans IDE, select File -> New Project.

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


      Note –

      For this step, you could also create a RESTful web service in a Maven web project by selecting Maven as the category and Maven Web Project as the project. The remaining steps would be the same.


    3. Type a project name, HelloWorldApplication, and click Next.

    4. Make sure that the Server is GlassFish Server (or similar wording.)

    5. Click Finish.

    The project is created. The file index.jsp appears in the Source pane.

  2. Right-click the project and select New; then select RESTful Web Services from Patterns.

    1. Select Simple Root Resource and click Next.

    2. Type a Resource Package name, such as helloWorld.

    3. Type helloworld in the Path field. Type HelloWorld in the Class Name field. For MIME Type, select text/html.

    4. Click Finish.

      The REST Resources Configuration page appears.

    5. Click OK.

      A new resource, HelloWorld.java, is added to the project and appears in the Source pane. This file provides a template for creating a RESTful web service.

  3. In HelloWorld.java, find the getHtml() method. Replace the //TODO comment and the exception with the following text, so that the finished product resembles the following method.


    Note –

    Because the MIME type produced is HTML, you can use HTML tags in your return statement.


    /**
     * Retrieves representation of an instance of helloWorld.HelloWorld
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("text/html")
    public String getHtml() {
        return "<html><body><h1>Hello, World!!</body></h1></html>";
    }
  4. Test the web service. To do this, right-click the project node and click Test RESTful Web Services.

    This step deploys the application and brings up a test client in the browser.

  5. When the test client appears, select the helloworld resource in the left pane, and click the Test button in the right pane.

    The words Hello, World!! appear in the Response window below.

  6. Set the Run Properties:

    1. Right-click the project node and select Properties.

    2. In the dialog, select the Run category.

    3. Set the Relative URL to the location of the RESTful web service relative to the Context Path, which for this example is resources/helloworld.


    Tip –

    You can find the value for the Relative URL in the Test RESTful Web Services browser window. In the top of the right pane, after Resource, is the URL for the RESTful web service being tested. The part following the Context Path (http://localhost:8080/HelloWorldApp) is the Relative URL that needs to be entered here.

    If you don’t set this property, the file index.jsp will appear by default when the application is run. As this file also contains Hello World as its default value, you might not notice that your RESTful web service isn’t running, so just be aware of this default and the need to set this property, or update index.jsp to provide a link to the RESTful web service.


  7. Right-click the project and select Deploy.

  8. Right-click the project and select Run.

    A browser window opens and displays the return value of Hello, World!!

See Also

For other sample applications that demonstrate deploying and running JAX-RS applications using NetBeans IDE, see The rsvp Example Application and Your First Cup: An Introduction to the Java EE Platform at http://download.oracle.com/javaee/6/firstcup/doc/. You may also look at the tutorials on the NetBeans IDE tutorial site, such as the one titled “Getting Started with RESTful Web Services” at http://www.netbeans.org/kb/docs/websvc/rest.html. This tutorial includes a section on creating a CRUD application from a database. Create, read, update, and delete (CRUD) are the four basic functions of persistent storage and relational databases.