Skip to main content

RESTful representation of "sakila" using GlassFish and NetBeans IDE

Posted by arungupta on January 24, 2008 at 4:44 AM PST

"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!

  1. Install MySQL & the sample database "sakila".
    1. Download and
      Install
      MySQL Community Server.
    2. Download sakila
      sample database
      .
    3. Install the database as described

      here
      .
    4. Start MySQL database by giving the command '
      mysqld-nt --user root 
      --console
      ' in bin directory on Windows or './bin/mysqld_safe'
      from MySQL directory on Unix flavors.
  2. Create the Project & Database Connection
    1. In NetBeans IDE, create a new Web project and name it as "sakila".
      Choose "GlassFish v2" as the "Server:".
    2. In the "Services" tab of NetBeans IDE, expand "Drivers" and add
      MySQL
      Connector/J
      driver if it does not exist already.
    3. Create a new new database connection by right-clicking on "Drivers" and
      specifying the parameters as shown below:



  3. Create the Persistence Unit
    1. 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:



    2. Click on "film" in "Available Tables" and click on "Add >" as shown
      below:







      Click on "Next >".
    3. Click on "Create Persistence Unit..." and take all the defaults as shown
      below:







      Click on "Create".
    4. Enter the package name as "sakila" as shown below:







      and click on "Finish".
    5. 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.
  4. Create RESTful Web service endpoint
    1. 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.
    2. Right-click on the project, select "New", "
      RESTful Web Services from 
      Entity Classes...
      ".
    3. Click on "Add >>", take all other defaults as shown
      below:







      click on "Next >", take all defaults and then "Finish".
  5. Test RESTful Web Services
    1. Right-click on the project and select "Test RESTful Web Services".
      The following web page is presented in the browser:



    2. 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:



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

Related Topics >>

Comments

:)

wasn't it pronounced like 'my see-kwall'?