Building a simple RESTful web service application using the NetBeans IDE

To create a JAX-RS application, perform the following steps:

  1. Launch the NetBeans IDE.
  2. In the main toolbar, navigate to File | New Project.
  3. On the New Project dialog screen, navigate to Maven | Web Application for building the RESTful web service. Proceed to the next screen by clicking on the Next button.
  4. In the Name and Location screen, enter Project Name, Project Location (for storing the source), Group Id, Version (for the Maven project), and Package (for the Java source files), as follows:
    • Project Name: rest-chapter3-service
    • Group Id: com.packtpub
    • Package: com.packtpub.rest.ch3.service

Refer to the following screenshot for the values used for this example:

If you are not familiar with Maven, enter the same values as you see in the preceding screenshot to save time and to stay focused. Otherwise, there is nothing preventing you from having your own values for any of the fields that you see in the wizard. After setting all the values, click on Next to continue to the Settings screen in the wizard.

  1. On the Settings screen, select GlassFish Server, which you installed along with the NetBeans IDE as Server for running your JAX-RS application. Then, click on Java EE 7 Web as the Java EE version for the application that you'll build.
  2. The server list on this screen may appear empty if you have not configured any server for the IDE yet. To add a new server reference, perform the following steps:
    1. Click on the Add button. In the Add Server Instance wizard, choose GlassFish as the server and click on Next to continue the wizard.
    2. Set Server Location to the folder where you installed GlassFish. Select Local Domain and click on Next to continue the wizard.
    3. On the Domain Name screen, enter domain1 in the Domain field (which is the default one) and localhost in the Host field. Click on Finish to complete the server creation.
  1. Now, the IDE will take you back to the Settings screen once again where you can choose GlassFish (that you added in the previous step) as the server and Java EE 7 Web as the Java EE version.
  2. You can now click on the Finish button to finish the project configuration wizard. NetBeans will now set up a Maven-based web project for you, as shown in the following screenshot:
  3. The next step is to build a simple RESTful web service implementation by using a POJO class to get a feel of the JAX-RS APIs.
  4. To build a POJO class, you can right-click on the project and navigate to New | Java Class in the menu. In the New Java Class editor, enter DepartmentService in the Class Name field and enter com.packtpub.rest.ch3.service in the Package field. This class will contain the service implementation for this example. We will add @Path("departments") to this class so that DepartmentService becomes a REST resource class and responds to the REST API calls with the URI path fragment, departments. Let's add a simple helloWorld() method to this class and add @Path("hello") to this method. Add the @GET annotation to designate this method to respond to the HTTP GET methods. The DepartmentService class now looks like the following:
package com.packtpub.rest.ch3.service; 
import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType;  
 
@Path("departments") 
public class DepartmentService{ 
  @GET 
  @Path("hello")   
  @Produces(MediaType.APPLICATION_JSON) 
  public String helloWorld(){ 
    return "Hello world"; 
  } 
} 
  1. To configure resources, add a REST configurations class, which extends javax.ws.rs.core.Application. This class defines the components of a JAX-RS application and supplies additional metadata if any. The configuration class looks like the following:
package com.packtpub.rest.ch3.jaxrs.service; 
 
import java.util.Set; 
import javax.ws.rs.core.Application; 
 
@javax.ws.rs.ApplicationPath("webresources") 
public class RestAppConfig extends Application { 
// Get a set of root resource and provider classes. 
  @Override 
  public Set<Class<?>> getClasses() { 
    Set<Class<?>> resources =  
      new java.util.HashSet<>(); 
    resources.add(com.packtpub.rest.ch3.service.
DepartmentService.class); return resources; } }
Specifying the application path

The @javax.ws.rs.ApplicationPath annotation that you see in the preceding code snippet identifies the application path that serves as the base URI for all the resources defined in this application.

Alternatively, you can define the application path in web.xml, as shown in the following code. However, to keep things simple, we will not use web.xml for configuring resources in this example:

<servlet> 
  <servlet-name>javax.ws.rs.core.Application 
  </servlet-name> 
  <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
  <servlet-name>javax.ws.rs.core.Application 
  </servlet-name> 
  <url-pattern>/webresources/*</url-pattern> 
</servlet-mapping> 
  1. With this step, we have finished building a very simple JAX-RS RESTful web service. To deploy and run the RESTful web service application, you can right-click on the rest-chapter3-service project and click on Run. This will build and deploy the application to the GlassFish server integrated with the NetBeans IDE.
  2. To test the desired REST API, right-click on the appropriate HTTP methods, as shown in the following screenshot, and select Test Resource Uri. This action will open up the default browser with the response content returned by the REST call. The URI for accessing the helloWorld RESTful web API will look like http://localhost:8080/rest-chapter3-service/webresources/departments/hello:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.59