RESTful representation of "sakila" using GlassFish and NetBeans IDE
Join the DZone community and get the full member experience.
Join For Free"sakila" is the sample database shipped with MySQL (pronounced as my ess-kew-ell). In the context of Sun Microsystems announcing the agreement to acquire MySQL, I'd like to dedicate this entry to show how this sample database can be exposed as a RESTful Web service endpoint and deployed on GlassFish using Jersey Tooling Plugin (0.4.1 with Jersey 0.4) in NetBeans IDE.
Lets get started!
- Install MySQL & the sample database "
sakila
".- Download and Install MySQL Community Server.
- Download sakila sample database.
- Install the database as described here.
- Start MySQL database by giving the command '
mysqld-nt --user root --console
' inbin
directory on Windows or './bin/mysqld_safe
' from MySQL directory on Unix flavors.
- Create the Project & Database Connection
- In NetBeans IDE, create a new Web project and name it as "
sakila
". Choose "GlassFish v2
" as the "Server:
". - In the "
Services
" tab of NetBeans IDE, expand "Drivers
" and add MySQL Connector/J driver if it does not exist already. - Create a new new database connection by right-clicking on "Drivers" and
specifying the parameters as shown below:
- In NetBeans IDE, create a new Web project and name it as "
- Create the Persistence Unit
- Right-click on the project and select "
New
", "Entity Classes from Database...
". In "Data Source
", select "New Data Source...
" and specify the values as shown below:
- Click on "
film
" in "Available Tables
" and click on "Add >
" as shown below:
Click on "Next >
". - Click on "
Create Persistence Unit...
" and take all the defaults as shown below:
Click on "Create
". - Enter the package name as "
sakila
" as shown below:
and click on "Finish
". - In the NetBeans project explorer, expand "
Configuration Files
" and open "persistence.xml
". Specify the username and password by replacing<properties/>
with the following fragment:
<properties>
<property name="toplink.jdbc.user" value="root"/>
<property name="toplink.jdbc.password" value=""/>
</properties>
Make sure to match the username and password to your MySQL installation.
- Right-click on the project and select "
- Create RESTful Web service endpoint
- In NetBeans IDE, click on "
Tools
", "Plugins
", "Available Plugins
", "RESTful Web Services
" and then click on "Install
". This installs the Jersey Tooling Plugin in the IDE. - Right-click on the project, select "
New
", "RESTful Web Services from Entity Classes...
". - Click on "
Add >>
", take all other defaults as shown below:
click on "Next >
", take all defaults and then "Finish
".
- In NetBeans IDE, click on "
- Test RESTful Web Services
- Right-click on the project and select "
Test RESTful Web Services
". The following web page is presented in the browser:
- Click on "
films
" and then on "Test
" as shown below:
Clicking on "Test
" button or the URL "http://localhost:8080/sakila/resources/films/
" shows the RESTful representation of the "Film
" table. The default representation shows 10 records from the table where each entry returns the "id
" of the film and a reference to the detailed entry.
You can view more entries (say 40) by giving the URL "http://localhost:8080/sakila/resources/films/?max=40
". Additional fields from the table can be displayed by adding getter methods to "converter.FilmRefConverter
" class such as:
@XmlElement
public String getTitle() {
return entity.getTitle();
}
to return the film title in addition to the fields already returned. The different columns in the table can be viewed by going to the "Services" tab, expanding the sakila database connection created earlier as shown below:
The modified output (with film title included) looks as shown below:
- Right-click on the project and select "
Here are few more ideas for you to explore:
- Create RESTful representations of other tables using the steps described above.
- Display the data from different tables in a jMaki-wrapped Yahoo or Dojo data table as explained in TOTD #10.
- Display the data retrieved from the database in a JSP page as described in Hello JPA World.
- Create a CRUD application using jMaki Data Table as described in TOTD #15 or Screencast #Web10.
A JRuby-on-Rails application using MySQL is explained here. TOTD #9 explains how JDBC connection pooling in GlassFish can be used for a JRuby-on-Rails application using MySQL.
The key message here is MySQL can be very easily used with GlassFish and NetBeans IDE makes it possible! Once MySQL becomes part of Sun, this integration is going to be much more seamless for the betterment of community.
All the entries on this blog using MySQL can be found here. And last but not the least, Welcome aboard MySQL!
A NetBeans project with all the source code can be downloaded from here. You will still need to setup the database connection and need to make sure the correct version of Jersey plug-in as well :)
Technorati: glassfish netbeans jersey mysql sakila jpa jmaki rubyonrails
Published at DZone with permission of Arun Gupta, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Apache Kafka vs. Message Queue: Trade-Offs, Integration, Migration
-
Build a Simple Chat Server With gRPC in .Net Core
-
Building A Log Analytics Solution 10 Times More Cost-Effective Than Elasticsearch
-
Playwright JavaScript Tutorial: A Complete Guide
Comments