days
1
3
hours
0
9
minutes
4
1
seconds
3
2
Getting some REST

How to export data to REST

Geertjan Wielenga
Rest image via Shutterstock

Standardisation, automated tasks and “content negotiation” –– there are plenty of reasons to use REST for exposing data. Geertjan Wielenga shows us how to get started using NetBeans in a few easy steps.

The architectural web development style known as “REST” is based on the HTTP protocol, wherein everything is a resource, accessed by means of a standardized interface based on standard HTTP methods. Within this context, a REST server provides access to resources, while a REST client accesses and modifies them. Resources are identified by global IDs, which are typically URIs.

Handily, REST enables resources to have different kinds of representations, such as text, XML, and JSON, so that REST clients can ask for a specific representation via the HTTP protocol by means of a process known as “content negotiation”.

The promise of REST is that you can expose data in a standardized way. Standardization is beneficial in that clients, whether on the desktop or on mobile devices, have a protocol by means of which your data can be accessed. Moreover, standardization enables tool providers to automate the tasks involved in exposing data as REST. To see how this is done, set up a database in NetBeans IDE and rapidly generate REST resources from its tables, as explained in detail in this article.

Above, a database has been registered in NetBeans IDE. Your task is to make data available to other departments within your organization. Rather than spending time coming up with internal protocols, REST gives you a globally accepted mechanism by means of which a server can expose data, while multiple clients can make use of it.

In NetBeans IDE, the process is as simple as a few clicks. The code that is generated is standardized in the REST specification. Though the specification is clear and describes everything needed succinctly, quite some code needs to be typed by hand. Manual, error-prone work can be avoided where standards exist. Below, the “RESTful Web Service from Database” template provides an entry point out of the box, no plugins need to be installed into NetBeans IDE to enable REST to work, nor are plugins needed for integrating the related technologies provided by Maven and GlassFish.

The template shown above allows a database to be selected, exposing all its tables to the developer. All the tables from the selected database are shown below. For each, a plain old Java object will be created, representing the related table. Fields, getters, and setters in Java will be generated into the class, which will conform to the JPA specification, annotated according to the annotations it provides. Such classes are known as “JPA entity classes”, each mapping to a table and defining its constraints via annotations provided by the Bean Validation API.

The result is shown below. For each table, e.g., ‘COUNTRIES’, a Java class, e.g., ‘Countries’, with a related REST web service, e.g., ‘CountriesFacadeREST’ is created.

Moreover, the logical view of the project provides entry points into each of the REST web services, so that they can be opened directly from NetBeans IDE.

The logical view above provides a clear structure, consisting of an overview of the JPA entity classes that map to tables in the database, together with the REST web services that will expose the underlying data.

Supporting the logical view, a graph view shows the dependencies managed by Maven. Each JAR file, pulled in from its repository, is shown in relation to the other JAR files in the application.

Accessing the data in the ‘COUNTRIES’ database is now possible, once the application is deployed. Tweaking the @Path annotation in the ‘Countries’ Java file is all that’s needed to create a REST resource such as the following:

http://localhost:8080/EmployeeRecordManager/webresources/countries

Clients accessing the URL above can then parse the payload and present it to the user. Changes can be made, depending on requirements, and submitted back to the database. What should be clear from these features is that standardization is not only of benefit to users of specifications such as Java EE.

“I work as a consultant for different customers, and not all of them have adopted NetBeans IDE as their standard IDE. However, I’ve converted more than a few to NetBeans IDE by showing them the code generation features, such as for JPA and RESTful resources, alone.” David Heffelfinger. Chief Technology Officer of Ensode Technology, LLC, a software consulting firm based in the greater Washington DC area.

Or, put another way, tool providers are as much users of standards as web application developers are. The unique features integrated into NetBeans to simplify the tasks of REST resource developers are made possible by the same standards enabling those developers to create their applications. NetBeans IDE is a free and open source development environment based on standards, such as those made available by Java EE. Start exporting your data to REST with NetBeans IDE and notice how simple the steps are and how powerful their results.

Author
Geertjan Wielenga
Geertjan is product manager of NetBeans IDE and lives in Amsterdam, the Netherlands. He blogs at blogs.oracle.com/geertjan.

Comments
comments powered by Disqus