NetBeans to Generate Simpler RESTful Web Services
NetBeans 7.0 has a slew of new features, including REST support.
Join the DZone community and get the full member experience.Join For Free
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.
here you see the customerconverter class annotated with xmlrootelement and xmlelement.
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.
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.
the jaxb annotations are directly in the entity classes instead. for example, the customer class is annotated with xmlrootelement.
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.
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.
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).
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.
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...
...to 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.
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.
Opinions expressed by DZone contributors are their own.