Sharing a service through Web Service
Web services are APIs which, in the case of this recipe, access some data over a network from
any platform and using any programming language.
In the world of cloud computing, web services have become an increasingly popular way for
companies to let developers create applications using their data. A good example of this is
Twitter. Thanks to exposition of Twitter data through web services, it has been possible to
create numerous Twitter clients on virtually all platforms. In this recipe, we will create a web
service that returns information from a database table; we will see that this information can
user with data access simplicity, when compared to XML, since it does not need a bunch of
tags and nested tags to work Getting ready
It is required to have NetBeans with Java EE support installed to continue with this recipe.
If this particular NetBeans version is not available in your machine, please visit:
We will use the GlassFish Server in this recipe since it is the only server that supports
Java EE 6 at the moment.
We also need to have Java DB configured. GlassFish already includes a copy of Java DB in
its installation folder. If you wish to learn how to configure Java DB, refer to Chapter 4, JDBC
It is possible to create this recipe if an existing database schema and an EJB application
exists. However, for the sake of brevity, we will use the sources from the previous recipes.
How to do it…
Right-click on the EJBApplication node, select New then Other then Web Services and
RESTFul Web Services from Entity Class….
- For Entity Classes: On Available Entity classes, select Manufacturer, click Add, and
- For Generated Classes: Leave all the fields with their default values and click Finish.
A new dialog, REST Resources Configuration, pops-up; select the first option and click OK.
How it works…
The REST resources configuration asks the user which way the RESTful resources should
be accessed, presenting the user with three different options. We have chosen to use
javax.ws.rs.core.Application because it is the standard in Java EE 6 and, thus,
increases the portability of the application, instead of the web.xml option. The second option
allows the developer to code their way through registering the resources and choosing the
To take a look at the generated files, expand the service package. Two java files are present:
AbstractFacade.java and ManufacturerFacadeREST.java.
Opening the ManufacturerFacadeREST.java will show that this file is actually a stateless
EJB created by the IDE that is used to interface with the database and retrieve information
NetBeans also automatically generates a converter for our ManufacturerResource. This
converter is used for creating a resource representation from the corresponding entity
instance. Those classes can be found in the converter package.
Using NetBeans to test the web services.
Testing the web service
Now that we have created a RESTful web service, we need to know if everything is working
correctly or not.
To test our web service, right-click EJBApplication and select Test RESTful Web Service.
NetBeans will be launched; deploy our application in GlassFish and then point the browser
to the web service.
When the Test RESTful Web Service page opens, click on the Test button on the right side.
Under Tabular View, it is possible to click in the URI and get the XML response from
Raw View, on the other hand, returns the entire response, as it would be handled by
It is also possible to change the format in which the response is generated. Simply click on the
drop-down Choose method to test from GET(applicatio n/xml) to GET(application/json) and
click Test. Then click on Raw View to get a glimpse of the response.
Latest posts by Krishna Srinivasan (see all)
- Difference Between FilterDispatcher and StrutsPrepareAndExecuteFilter in Struts 2 - December 13, 2013
- Struts 2 Control Tags - December 13, 2013
- Struts 2 Generator Tag Example - December 13, 2013