The Java EE 6 Tutorial, Volume I

Example: Adding on to the Simple Hello World RESTful Web Service

This section discusses the simple RESTful web service that is included with the tutorial examples in the directory jaxrs/HelloWorld3. This example was created by following the steps similar to those described in Creating a RESTful Web Service Using NetBeans IDE.

HelloWorld3 Example: Discussion

This example takes the simple Hello World application discussed in the previous section and adds to it. In this example, there are methods for getting a user's name, and then the name is appended to the Hello World greeting. An annotation that wasn't used in the previous example, @QueryParam, is used in this example.

In this example, there is a simple RESTful web service that returns HTML messages. To accomplish this task, you would first create a class that uses Java Architecture for XML Binding (JAXB). This class represents the HTML message in Java (RESTGreeting.java), then creates a RESTful web service that returns an HTML message (HelloGreetingService.java.)

The JAXB class that represents the HTML message gets the message and the name. This file, RESTGreeting.java, is basic Java code that creates a new instance of RESTGreeting and the getter and setter methods for its parameters.

The RESTful web service that returns an HTML message is in the file HelloGreetingService.java. You may notice that method that is annotated with JAX-RS annotations is similar to the one described in the previous example, however, this example adds an @QueryParam annotation to extract query parameters from the Query component of the request URL. The following code example shows the JAX-RS-annotated method:

@GET
    @Produces("text/html")
    public RESTGreeting getHtml(@QueryParam("name")String name) {
        return new RESTGreeting(getGreeting(), name);
    }     
    private String getGreeting(){
        return "Hello ";
    }

ProcedureTesting the HelloWorld3 Example

  1. Open the project javaeetutorial/jaxrs/HelloWorld3 in NetBeans IDE.

  2. Right-click the project node, HelloWorld3, and select Test RESTful Web Services.

  3. Click the helloGreeting service in the left pane.

  4. Enter a name in the name text field.

  5. The Get(text/html) method is selected by default. Click Test.

  6. The response Hello name, displays in the Response pane, under the Raw View tab.

ProcedureDeploying and Running the HelloWorld3 Example

Before You Begin

The application's Run properties must be set to run the RESTful web service. For the provided application, this task has been completed. For future reference, right-click the project node, select Properties, then select Run, and enter the Relative URL. For this example, you would enter /helloGreeting.

  1. Right-click the project node, HelloWorld3, and select Deploy.

  2. Right-click the project node, HelloWorld3, and select Run.

    The Run property does not specify a particular name, so none is shown in the browser window when it displays. The browser window simply shows the message Hello.

  3. Append a name to the URL in the web browser, so that the URL looks like this: http://localhost:8080/HelloWorld3/helloGreeting?name=your_name.

  4. The message Hello and the name your_name display in the browser.