CreatingZemblyServiceUsingNB

Using Netbeans to develop and deploy RESTful Web Service as a service onto Zembly.com

In this article you will learn how to

  • Develop a HelloWorld RESTful web service using Netbeans (6.7M3/6.5) RESTful web service support.
  • Create a wadl file for the above service using Netbeans WADL designer (using the plugin from update center)
  • Deploy the service onto Zembly using Build your own API (BYOAPI) feature.

Contents


Preparation:

Using NB6.7M3

  • JDK 1.6
  • Latest NetBeans 6.7M3
  • Run NB 6.7 M3, then install WADL designer plugin from the NB Plugin manager
  • From the menu bar Tools->Plugins->Available Plugins search for 'Wadl'
  • Install WADL design plugin


Image:installwadl_67_CreatingZemblyServiceUsingNB.png

Using NB 6.5:

  • JDK 1.6
  • Latest NetBeans 6.5
  • Run NB 6.5 IDE, then install WADL designer plugin from the NB Plugin manager
  • From the menu bar Tools->Plugins->Settings (See screenshots below), create a new update center entry name

'NB Dev UC', enter in the URL http://deadlock.netbeans.org/hudson/job/nbms-and-javadoc/lastStableBuild/artifact/nbbuild/nbms/updates.xml.gz

  • Press OK then switch to tab 'Available Plugins' tab, then click 'Reload Catalog' button to see WADL designer

and model modules.

  • Select both the modules and click 'Install' button. This will install the WADL designer.

(See below)
Image:wadl_design_uc_for_65_CreatingZemblyServiceUsingNB.png


Image:installwadlplugin_CreatingZemblyServiceUsingNB.png

1. How to create a service using Netbeans

Open Netbeans IDE, then on the "Projects" tab, right click to create new "Java Web" project 'api'


Image:pic1_CreatingZemblyServiceUsingNB.png

Select Web Application


Image:pic2_CreatingZemblyServiceUsingNB.png

Enter project name 'api'


Image:newproject_CreatingZemblyServiceUsingNB.png

Select GlassFishV2 server and click "Finish"


Image:selectserver_CreatingZemblyServiceUsingNB.png

Now we need to create a RESTful web service 'GreetingService.java' that would return a greeting JSON message. Lets represent the JSON message in java using JAXB class 'Greeting.java'. To do this right click on the project 'api', select 'New->Java Class'. On the 'New Class' wizard enter 'Class Name' as 'Greeting' and 'Package' as 'com.travnest.api' and click 'Finish'. Modify the file 'Greeting.java' as shown below.

package com.travnest.api;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;

/**
 *
 * @author ayubkhan
 */
@XmlRootElement(name = "greeting")
public class Greeting {

    private String message;
    private String name;

    /**
     * Creates a new instance of Greeting.
     *
     */
    public Greeting() {
    }

    /**
     * Creates a new instance of Greeting.
     *
     * @param message - greeting message
     * @param name - person name
     */
    public Greeting(String message, String name) {
        this.message = message;
        this.name= name;
    }

    /**
     * Getter for message.
     *
     * @return value for message
     */
    @XmlElement
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * Getter for name.
     *
     * @return value for person's name
     */
    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


Create New RESTful web service class by right clicking project and selecting New->RESTful Web Services from Patterns


Image:pic5_CreatingZemblyServiceUsingNB.png

Select singleton pattern


Image:selectsingleton_CreatingZemblyServiceUsingNB.png

Modify Class name


Image:sayhelloservice_CreatingZemblyServiceUsingNB.png

Change the GreetingService source as follows

package com.travnest.api;

import java.util.Calendar;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

/**
 * Greeting REST Web Service
 *
 * @author ayubkhan
 */

@Path("sayHello")
public class GreetingService {
    @Context
    private UriInfo context;

    /** Creates a new instance of GreetingService */
    public GreetingService() {
    }

    /**
     * Retrieves representation of an instance of GreetingService
     *
     * @param - name (a query string)
     * @return greeting
     */
    @GET
    @Produces("application/json")
    public Greeting getJson(@QueryParam("name") String name) {
        return new Greeting(getGreeting(), name);
    }

    private String getGreeting() {
        return "Good " + (Calendar.getInstance().get(Calendar.AM_PM) == Calendar.AM ? "Morning" : "Afternoon");
    }

}


Now test the service

Note: Change the /WEB-INF/web.xml's ServletAdaptor's url-pattern from "/resources/*" to "/*", before testing this service


Image:testservice_CreatingZemblyServiceUsingNB.png


Test client showing the result after testing the service "sayHello"


Image:jsonoutput_CreatingZemblyServiceUsingNB.png


Verify the wadl generated by the RESTful web service runtime by pointing browser to http://localhost:8080/api/application.wadl


Image:servicewadl_CreatingZemblyServiceUsingNB.png

2. Deploy the travnest.war file to the domain "travnest.com"

Test the service by pointing your browser to

http://travnest.com/api/sayHello?name=ayub

The browser will display the json content as shown below

{"message":"Good Afternoon","name":"ayub"}

3. How to create a WADL for the above service (needed to deploy service to zembly.com)

Right-click project 'api/Web Pages', New->Other


Image:newwadl_CreatingZemblyServiceUsingNB.png

Select 'Wadl' document


Image:selecttype_CreatingZemblyServiceUsingNB.png

Enter file name


Image:enterfilename_CreatingZemblyServiceUsingNB.png

Enter base url, then click "Finish"


Image:enterfilename1_CreatingZemblyServiceUsingNB.png

Move the file from "Source Packages" to "Web Pages" (Optional)

New wadl created, now replace the default content with the content

from the http://localhost:8080/api/application.wadl as shown below


Image:changesource_CreatingZemblyServiceUsingNB.png

Switch editor tab to "Design"


Image:wadldesign_CreatingZemblyServiceUsingNB.png

Change the base url to "http://travnest.com/api/" your real domain


Image:changebaseurl_CreatingZemblyServiceUsingNB.png

Add documentation title to service 'sayHello'


Image:servicedoctitle_CreatingZemblyServiceUsingNB.png

Add documentation description to service 'sayHello'


Image:servicedoc_CreatingZemblyServiceUsingNB.png

Add documentation title to method 'getJson'


Image:methoddoctitle_CreatingZemblyServiceUsingNB.png


Add documentation description to method 'getJson'


Image:methoddoc_CreatingZemblyServiceUsingNB.png

After save the generated source can be viewed by clicking the "Source" tab (see generated source below)

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xsi:schemaLocation="http://research.sun.com/wadl/2006/10 https://wadl.dev.java.net/wadl20061109.xsd"
            xmlns="http://research.sun.com/wadl/2006/10">
    <resources base="http://travnest.com/api/">
        <doc title="sayHello greeting service.">This service returns Good Morning/Afternoon xyz, based on the time of the day.</doc>
        <resource path="sayHello">
            <method name="GET" id="getJson">
                <doc title="getJson">This is a simple test service that returns a greeting in json.</doc>
                <request>
                    <param type="xs:string" style="query" name="name" default=""/>
                </request>
                <response>
                    <representation mediaType="application/json"/>
                </response>
            </method>
        </resource>
    </resources>
</application>


4. Publish your service to Zembly.com

Add travnest.com as a new domain to Zembly.com


Image:adddomain_CreatingZemblyServiceUsingNB.png

Claim this domain by generating a file that needs to be copied over to root directory of http://travnest.com (security requirement by Zembly.com)


Image:claimdomain_CreatingZemblyServiceUsingNB.png

Now add the API (using the WADL we created using Netbeans6.5 earlier)


Image:pic32_CreatingZemblyServiceUsingNB.png

Validate your API


Image:pic33_CreatingZemblyServiceUsingNB.png

Click finish to submit the API


Image:pic34_CreatingZemblyServiceUsingNB.png

Click "getJson" service


Image:pic35_CreatingZemblyServiceUsingNB.png

sayHello.getJson service description


Image:pic36_CreatingZemblyServiceUsingNB.png

sayHello.getJson usage


Image:pic37_CreatingZemblyServiceUsingNB.png


Create a new data service out of our existing "sayHello.getJson" service


Image:pic40_CreatingZemblyServiceUsingNB.png

Edit this new service


Image:pic41_CreatingZemblyServiceUsingNB.png

using code completion fill out the javascript service call


Image:codec1_CreatingZemblyServiceUsingNB.png


Image:codec2_CreatingZemblyServiceUsingNB.png

Add a parameters to the new service


Image:addparams_CreatingZemblyServiceUsingNB.png

(Optional) Add the code snippet after searching for "sayHello" service


Image:search_CreatingZemblyServiceUsingNB.png

Test this service


Image:pic47_CreatingZemblyServiceUsingNB.png

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo