Jeff has posted 11 posts at DZone. View Full User Profile

NetBeans to Generate Simpler RESTful Web Services

  • submit to reddit
In the 7.0 version of NetBeans IDE, currently available as a development build, there are two changes to Java EE 6 RESTful web services generated from entity classes that greatly simplify the resulting application:
  • JAXB annotations can be added directly to JPA entity classes. This removes the need for converter classes.
  • Services are generated as EJB session facades. This reduces the number of generated service classes and simplifies their code.

JAXB annotations in Java code indicate the XML structure that should be generated from that code. Previously, when you generated a RESTful web service from entity classes, NetBeans IDE created converter classes that contained these annotations.

Starting with NetBeans IDE 7.0, you have the option of generating JAXB annotations directly in the JPA entity classes. This not only leads to cleaner code, but should reduce problems that arise between static JPA and runtime JAXB-generation of XML, such as loops in the reference creating infinitely deep XML. When cases arise where you do have to modify the JPA classes by hand, this should be much easier.

Here is the structure of an EE 6 RESTful service created with NetBeans IDE 6.9.1, showing the converter classes.

EE6 RESTful project with converter classes

Here you see the CustomerConverter class annotated with XmlRootElement and XmlElement.

CustomerConverter class with JAXB annotations

DiscountCode also has an @XmlTransient annotation for the getCustomerCollection method. The @XmlTransient annotation is necessary because Customer and DiscountCode are mutually dependent. The @XmlTransient annotation prevents JAXB Marshaller from failing due to a cyclic dependency.

Note: If you create EE6 RESTful web services from existing entity classes, make certain that they have the necessary JAXB annotations. You may need to add them by hand. In this case, be careful of cyclic dependencies and annotate any affected methods with @XmlTransient.

@XmlTransient annotation

Here is the structure of an EE 6 RESTful service created with a development build of NetBeans IDE 7.0. Note the absence of converter classes.

Project structure for RESTful service without converter classes

 The JAXB annotations are directly in the entity classes instead. For example, the Customer class is annotated with XmlRootElement.

 Entity class with JAXB annotations

When you use NetBeans IDE 7's RESTful Web Service from Database wizard, JAXB annotations are automatically added to the JPA entity classes. You cannot chose not to. However, when you use the Entity Classes from Database wizard, you have the choice whether or not to add JAXB annotations to the entity classes.

 Note: NetBeans IDE 7.0 also lets you add JAXB annotations to EE 5 entity classes. However, RESTful web services generated from EE 5 entity classes still have converter classes.

A session facade is a design pattern advertised in the Enterprise BluePrints program. As stated in the Core J2EE Pattern Catalog, it attempts to resolve common problems that arise in a multi-tiered application environment, such as:

  • Tight coupling, which leads to direct dependence between clients and business objects
  • Too many method invocations between client and server, leading to network performance problems
  • Lack of a uniform client access strategy, exposing business objects to misuse

A session facade abstracts an application's underlying business object interactions and provides a service layer that exposes only the required functionality. Thus, it hides from the client's view the complex interactions between the objects. The session bean also manages the life cycle of business objects. The session bean creates, locates, modifies, and deletes objects as required by the workflow.

When NetBeans IDE does not create session facades, the IDE creates separate service classes for entities and collections of entities. The following illustration shows the project structure of an EE 6 RESTful service created from a database using NetBeans IDE 6.9.1.

 Project structure not using facades

By creating services as session facades, the development version of NetBeans IDE only needs to create one service class for each entity class. The following illustration shows the project structure of an EE 6 RESTful service created from the same database as the NetBeans 6.9.1 project, but with the service classes generated as session facades.

Project structure for RESTful service classes as session facades

Note that NetBeans IDE creates an abstract session facade class. This abstract class contains the code used by all service classes for managing entities. This code includes criteria queries as defined by the EE 6 Criteria API (JPA 2.0).

Abstract facade class

The other service classes extend the abstract session facade and call on its methods for a specific entity class. Here, the CustomerFacadeREST class calls the methods of AbstractFacade, replacing the generic class parameters with references to the Customer entity class. 

First part of CustomerRESTFacade

The service classes are easy to read, consisting of HTTP method-annotated method calls that refer back to the abstract class. Compare the CustomerFacadeREST session facade, which is 89 lines long...

Second part of CustomerRESTFacade the CustomerResource class generated by NetBeans IDE 6.9.1.  CustomerResource is 211 lines long. It includes not only GET, PUT, and DELETE methods, but all the entity management code and dynamic instances of the other service classes.

CustomerResource part 1

CustomerResource part 2

CustomerResource part 3

Here you can see quite clearly that the use of session facades results in simpler code, and a lot less of it! Customizing and maintaining this code should also be much easier.

Published at DZone with permission of its author, Jeff Rubinoff.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)



Clay Press replied on Mon, 2010/10/18 - 9:26pm

Jeff,  very helpful explanation.  of things to come. this technical documentation rocks.  Clay

Angelo Quaglia replied on Sat, 2010/11/13 - 2:44pm

Very helpful indeed. Many thanks!

Fernando Estrada replied on Thu, 2010/12/16 - 5:11pm

Thanks Jeff, I can use this. -Fret

Jeff Rubinoff replied on Wed, 2011/03/09 - 9:07am

I've added a little more information about JAXB annotations in entity classes, specifically @XmlTransient and its role in preventing JAXB Marshaller from crashing due to cyclic dependencies.

Krish Perumaal replied on Mon, 2011/05/16 - 10:12am

This really helped me when I was dabbling with this new NetBeans 7.0

Francesco Perissin replied on Fri, 2011/08/26 - 2:21pm

This works great! In addition it may be useful to add that in order to make CRUD operations from inside a Servlet, it's enough to inject the stateless bean in the servlet with:

@EJB private CustomerFacadeREST customerFacade;

which is ready to be used as an Entity Access Object (EAO) without need to care of the EntityManager.

Simon Jarslev replied on Fri, 2012/02/24 - 2:51pm

Hi, i am quite new to Netbeans let and Java too. The Pile of Api's IDE's, plugins and so on. I have been trying to create my own application with a MySQL database, Glassfish server and all of it based on the RESTful webservices guide on

When i test the RESTful webservices, the Get method (Reuest) just return a http 500 error. Well atleast i got something returned :D

 After reading this article i will try venture down into the deep jungle of automated code, and see if i might be able to locate anything.

If the MySQL is configured wrong, wouldn't it give some sort of error when i tried some queries out in the SQL Editor in Netbeans?


Carla Brian replied on Sun, 2012/04/01 - 5:52pm

Thank you for sharing this one. This could really be useful. It is a simpler way actually. - Naperville Dentists

Matt Coleman replied on Tue, 2012/04/03 - 4:04am

as usual this is great information

graphic designer buffalo

Mateo Gomez replied on Wed, 2012/04/04 - 8:08am in response to: Simon Jarslev

 I'm a nebie too and you have a great point.

mexican dessert

Jason Bourne replied on Mon, 2012/10/22 - 12:56am

A good example of content presentation. A piece of information from you every now and then is really great. Everything was so well coordinated and looked so aesthetically pleasing to the eye. Clickuplink Bookmarking

Jason Bourne replied on Mon, 2012/11/05 - 12:40pm

I would like to thank you for the efforts you have made while writing this post. I am hoping for the best work of the same from you in future
Click here

Gerard Bondt replied on Sun, 2013/05/05 - 3:16pm

Hello, I try to consume the EJB session façade EE 6 web service on Android (Android SDK ADT). 

I'm having issues with the generated persistence.xml file which Android seems to ignore in src\META-INF.


Any ideas?


Cata Nic replied on Mon, 2013/09/02 - 4:58pm

 How is generated the net services in the end of the process? I've read the entire tutorial but I still have some problems with the understanding of the stages.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.