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.
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
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.
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'
Select Web Application
Enter project name 'api'
Select GlassFishV2 server and click "Finish"
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
Select singleton pattern
Modify Class name
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
Test client showing the result after testing the service "sayHello"
Verify the wadl generated by the RESTful web service runtime by pointing browser to http://localhost:8080/api/application.wadl
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
Select 'Wadl' document
Enter file name
Enter base url, then click "Finish"
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
Switch editor tab to "Design"
Change the base url to "http://travnest.com/api/" your real domain
Add documentation title to service 'sayHello'
Add documentation description to service 'sayHello'
Add documentation title to method 'getJson'
Add documentation description to method 'getJson'
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
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)
Now add the API (using the WADL we created using Netbeans6.5 earlier)
Validate your API
Click finish to submit the API
Click "getJson" service
sayHello.getJson service description
sayHello.getJson usage