Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

Using the NetBeans IDE for RESTful Web Services

William Brogden delves into how to use the NetBeans IDE and JAX-RS API to create RESTful Web services.

The integrated development environment landscape is dominated by two open source offerings, NetBeans and Eclipse....

Today we will look at how one of them, NetBeans, is offering support for RESTful Web services.

NetBeans stems from a commercial IDE company purchased by Sun Microsystems and released as open-source in 2000. NetBeans is written in Java, giving it a very wide user base. Like Eclipse, NetBeans has a multitude of add-on modules with support for many languages and design techniques. NetBeans provides support for many of Sun's favorite technologies including the  Glassfish open-source Java EE 5 application server. Although the server is the core of the Glassfish development community, there are many projects which have an independent existence. Some projects are definitely on the cutting edge of Web service technology. One of these is an implementation of  JAX-RS, the Java API for RESTful Web Services or JSR 311, called Jersey. I was delighted to discover that the Jersey implementation has progressed to the point that it is a plug-in addition for the latest NetBeans, version 6.0, released in December 2007.

Installing NetBeans

I downloaded and installed NetBeans 6.0 - this is a rather large package in spite of the fact that it comes with only a minimum set of plug-ins. The plug-in manager function provides an elegant system for keeping up with updates and for picking the plug-ins you need. The installation includes the Glassfish server and the installation will detect other servers in your system. NOTE - although NetBeans works with Java JDK 5, upgrading to JDK 6 before installing NetBeans would be a great idea.

Adding RESTful Web service support

Updates to NetBeans and adding new functions is handled by the plug-in manager which connects to the central repository. I added the plug-in named "RESTful Web Services" and also updated several other modules. This plug-in is considered a beta version, corresponding to the Jersey version 0.5.1, so it is still in early stages of development.

I created a new RESTful Web Service project in NetBeans by following the Jersey example cited in the Web resources below. This mainly involved filling in the name that will appear in  URIs, Java package name and class name. The purpose of this service is to examine an input word and return a list of words that sound similar based on the  Metaphone phonetic coding algorithm. I specified that the URI path to this resource will be "lookup."

The JAX-RS specification always uses the word "resource" to emphasize the REST style of thinking about a service, as opposed to the  SOAP and  JAX-RPC style of thinking about services as activating a named procedure. The Java code the IDE created from the RESTful pattern uses this convention in creating skeleton code for the "LookupResource" class as seen below. Each request causes creation of a new instance of LookupResource. The "@Path" annotation tells the Jersey runtime servlet to map "lookup" in a URI to this service class. The "@HttpContext" annotation causes the runtime servlet to automatically "inject" a UriInfo object reference which carries all of the request URI information when an instance of LookupResource is created.

 @Path("lookup") public class LookupResource { @HttpContext private UriInfo context;

In the following Java code I have left the comment generated by NetBeans in creating the code skeleton. The "@GET" and "@ProduceMime" annotations tell the framework that this method can handle HTTP GET requests and return a HTML formatted String. Naturally it is up to the programmer to write the internal details of such methods. This example shows my first step, verifying that the query parameter value can be extracted from the UriInfo object.

 /** * Retrieves representation of an instance of com.wbrogden.phonetic.LookupResource * @return an instance of java.lang.String */ @GET @ProduceMime("text/html") public String getHtml() { // the context contains all query information String word = context.getQueryParameters().getFirst("match") ; return "Match String " + word ; }

The "@ProduceMime" annotation is used to locate the resource provider that is the closest match to the type specified in the request. Using the NetBeans "run" command for this project started my  Tomcat server. Startup messages from Tomcat showed that when the framework servlet started, it searched for implementing classes. Sending my browser to the following URI:

 http://localhost:8080/MetaPhone1/resources/lookup?match=word

Generated the expected text "Match String word" response, verifying that the framework was able to locate my service class, create an instance, and call the getHtml() method when the URI uses the query formatting style. However, further experimentation revealed that I could not get the system to recognize a URI that used the REST hierarchical style instead of query style formatting. The following URI should pass the "someword" text to the lookup resource, but Tomcat reported an error.

 http://localhost:8080/MetaPhone1/resources/lookup/someword

There are plenty of examples of support for the hierarchical style in the stand-alone version of the Jersey download so it looks like I still have some work to do to figure out how to get the NetBeans plug-in to do the same.

Netbeans provides plenty of support for programming and debugging. For example, when editing the getHtml() method code, the editor showed me all of the methods that the "context" object supports. A great feature for Web related programming is the ability to record the complete  HTTP request and response text.


 

This was last published in March 2008

Dig Deeper on REST and microservices

PRO+

Content

Find more PRO+ content and other member only offers, here.

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

Which open source offering do you prefer?
Cancel

-ADS BY GOOGLE

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide

SearchWinDevelopment

Close