By Ed Ort and Carol McDonald,
March 2009
Here's how to build the web application and its RESTful web services. As you build the application and web services, you'll see that a lot of the coding is automatically generated by the NetBeans IDE.
If you haven't already done so, download and install the following:
A lot of the coding for the application is automatically generated by the NetBeans IDE.
Figure 7. MySQL Server Database in the Database List
Note that the MySQL Connector/J JDBC Driver, which is necessary for communication between Java platforms and the MySQL database protocol, is included in NetBeans IDE 6.5.
Set the properties of the MySQL server database as follows:
The MySQL Connector/J JDBC Driver, which is necessary for communication between Java platforms and the MySQL database protocol, is included in the NetBeans IDE.
Figure 8. MySQL Server Basic Properties
localhost as the default server host name and 3306 as the default server port number.mysqld in the bin folder of the MySQL installation directory.mysqladmin in the bin folder of the MySQL installation directory. If the command is mysqladmin, in the Arguments field, type -u root stop to grant root permissions for stopping the server. The Admin Properties tab should look similar to Figure 9.
Figure 9. MySQL Server Administration Properties
petcatalog database as follows:
petcatalog. This will open a New Database Connection dialog box. Click OK to accept the displayed settings. petcatalog database in the list of drivers, as shown in Figure 10.
Figure 10. Driver for the petcatalog Database
Figure 11. Creating a Table in the Database
(Ctrl+Shift+E) in the SQL toolbar above the SQL editor.
You can view the table you just created. Right-click the Tables node below the petcatalog database in the Services window. You should see the item table under the Tables node. You can expand the item table node to see the table columns, indexes, and any foreign keys, as shown in Figure 12.
Figure 12. An Expanded Table Node
You can view the contents of a table or column by right-clicking the table or column and selecting View Data as shown in Figure 13.
Figure 13. Viewing the Contents of a Table
catalog in the Project name field and a project location in the Project Location field. Check the Set as Main Project checkbox. Click the Next button. This opens the Server and Settings page of the New Project wizard.catalog project folder as shown in Figure 14. The project folder contains all the Java source files and project metadata, such as the project's Ant build script. The IDE opens the catalog project and opens the welcome page, index.jsp, in the source editor window. You can view the logical structure of the project in the Projects window (Ctrl-1), and the project file structure in the Files window (Ctrl-2) of the IDE. Figure 14. The catalog Project and Welcome Page
images folder from the web directory in the completed catalog application to the web directory in the web application that you are building. You should see images in the web\images folder such as the cat shown in Figure 15.Figure 15. An Image of a Cat
Generate entity classes from the database. One of the powerful features of NetBeans IDE 6.5 is that it enables you to quickly create Java Persistence API entity classes from tables in a database. Here's how to generate entity classes from the tables that you created in the MySQL database:
catalog project node for the application and select New. Then select Entity Classes from Database. This opens the New Entity Classes from Database wizard.jdbc/petcatalog in the JNDI Name field and select jdbc:mysql://localhost:3306/petcatalog from the Database Connection drop-down list. Your entries should look like those in Figure 16. One of the powerful features of NetBeans IDE 6.5 is that it enables you to quickly create Java Persistence API entity classes from tables in a database.
Figure 16. Creating a Data Source
item table in the petcatalog database appears in the Available Tables listbox, as shown in Figure 17.
Figure 17. Selecting Tables to Generate Entity Classes
item table, then the Add button in the Database Tables page. This moves the item table in the Available Tables listbox to the Select Tables listbox. Click the Next button. This opens the Entity Classes page of the New Entity Classes from Database wizard.modelin the Package field. Make sure that the checkbox labeled Generate Named Query Annotations for Persistent Fields is selected. The Database Tables page should look as shown in Figure 18.
Figure 18. Specifying Tables and Entity Classes
In response, the IDE generates an entity class for the item table in the petcatalog database. You can see this in the Projects tab of the IDE by expanding the Source Packages node for the catalog project and then expanding the model node. This is shown in Figure 19.
Figure 19. New Entities Generated From Database Tables
If you open the generated class, you'll see that the IDE generated the Java code for the class including all the needed annotations.
Generate RESTful web services from the entity class you just created. Another powerful feature of NetBeans IDE 6.5 is that it enables you to quickly create RESTful web services from entity classes. Here's how to generate the RESTful web services:
Item entity class appears in the Available Entity Classes listbox, as shown in Figure 20. Another powerful feature of NetBeans IDE 6.5 is that it enables you to quickly create RESTful web services from entity classes.
Figure 20. Selecting Entity Classes to Generate RESTful Web Services
Item entity class, then the Add button in the Entity Classes page. This moves the Item entity class in the Available Entity Classes listbox to the Select Entity Classes listbox. Click the Next button. This opens the Generated Classes page of the New RESTful Web Services from Entity Classes wizard as shown in Figure 21.
Figure 21. Generated RESTful Web Services Classes
In response, the IDE creates the RESTful web services. The IDE uses the container-item pattern to generate the resource classes. For example, for the Item entity class, the IDE generates a container resource called ItemsResource and an item resource called ItemResource. Also, for each resource class, the IDE generates a converter class, that is, ItemsConverter and ItemConverter. Each converter class is used to generate the resource representation (XML or JSON) from the corresponding entity instance.
You can see the generated RESTful web services in the Projects tab of the IDE by expanding the Source Packages node for the catalog project and then expanding the RESTful Web Services node. Expand each RESTful web services node to display its HTTP methods and sub-resource locator methods, as shown in Figure 22. The value in the square brackets, [/items/], for the ItemsResource RESTful web service is the value for the URI template.
Figure 22. RESTful Web Services in the catalog Project
You can display the source file for a RESTful web service by double-clicking on its node.Test the RESTful web services using the test facilities of NetBeans IDE 6.5. Here's how to test the RESTful web services:
NetBeans IDE 6.5 provides a facility to test RESTful web services.
Figure 23. Testing RESTful Web Services
items node in the left side of the page. In response, the browser displays a list of parameters for testing the items service, as shown in Figure 24.
Figure 24. Selecting Testing Parameters
You can set the following parameters:
Figure 25. Test Results
Notice the following tabs in the Test Output section:
For example, Figure 26 shows the raw view of the JSON output returned in the items service test.
Figure 26. Raw View of JSON Output
Here's how to run the JavaFX client that accesses the RESTful web services for the pet catalog application.
<install_dir>\catalogclient, where <install_dir> is the directory where you expanded the package. For example, if you extracted the contents to C:\ on a Windows machine, then your newly created directory should be at C:\catalogclient. catalogclient project as follows:
catalogclient directory and click the Open Project button.In response, the IDE opens the catalogclient project. The project folder contains all the JavaFX source files as shown in Figure 27. You can view the logical structure of the project in the Projects window (Ctrl-1), and the project file structure in the Files window (Ctrl-2) of the IDE.
Figure 27. catalogclient Source Files
catalogclient project and select Properties. This opens the Project Properties dialog box. Notice that the Main Class field is already set to catalog.Main. Leave this default setting.Figure 28. Application Execution Model Choices
The radio buttons and their meanings are as follows:catalogclient project in the Projects window and selecting Run Project. The NetBeans IDE compiles the web service client. Depending on the Application Execution Model you selected, you should see the UI for the pet catalog application displayed in one of the following ways:
This article showed you how to use the NetBeans IDE with GlassFish and MySQL to create a RESTful web service that accesses an online catalog of pets. JAX-RS provides a standardized API for Java developers to build RESTful web services. GlassFish supports JAX-RS and its production-ready reference implementation, Jersey. In addition, GlassFish has a built-in persistence manager, the Java Persistence API.
These capabilities, combined with the simplicity of querying and manipulating MySQL databases, make it easy to build a RESTful web service that accesses a MySQL database with data persistence. It also makes it easy to create web applications that access web services. NetBeans IDE 6.5, which integrates MySQL and GlassFish, includes features that make it even easier to build web services and web applications that use web services.
This article also showed you how to use the NetBeans IDE with JavaFX support to run a JavaFX client that accesses a RESTful web service. Java FX is a platform that simplifies the development of RIAs -- web applications that use rich media types such as video, audio, and graphics -- that can run in a wide variety of devices, anywhere from handsets to laptops to desktops. In many cases, you can compile and run the same JavaFX application code in a mobile environment, in a desktop environment, or in a browser.
JavaFX provides a declarative scripting language, JavaFX Script, that makes it easy to program in a visual context. In addition, JavaFX provides a rich set of libraries that makes it easy to include graphics, media, and web services in an application. And you can use any Java library in a JavaFX application. These features allow you to create highly expressive UIs, including those that access web services, quickly and easily. NetBeans IDE with JavaFX support makes it easy to build, preview, and debug JavaFX applications. It also makes it easy to run JavaFX applications on the desktop, in the browser, as well as in a mobile environment.