January 24, 2008

RESTful representation of “sakila” using GlassFish and NetBeans IDE

Categories: netbeans, webservices

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

        <property name="toplink.jdbc.user" value="root"/>
        <property name="toplink.jdbc.password" value=""/>

      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:

      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 #

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

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #112: Exposing Oracle database tables as RESTful entities using JAX-RS, GlassFish, and NetBeans
  2. TOTD #99: Creating a Java EE 6 application using MySQL, JPA 2.0 and Servlet 3.0 with GlassFish Tools Bundle for Eclipse
  3. JRuby on Rails, NetBeans 6 and GlassFish V2 – Simplified Steps
  4. TOTD #93: Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3 – A simple Servlet 3.0 + JPA 2.0 app
  5. TOTD #38: Creating a MySQL Persistence Unit using NetBeans IDE


  1. Is this Jersey 0.5 or 0.6?

    Comment by Alexis MP — January 24, 2008 @ 12:55 am

  2. Great tutorial, i always like to read your daily blog. You should add a download link for the whole netbeans-project in this blog.

    Comment by Siggi — January 24, 2008 @ 1:03 am

  3. ALexis, This is using RESTful Web services plugin 0.4.1 with Jersey 0.4 (as described in the META-INF).

    Siggi, I’m working on upload the NetBeans project.

    Comment by Arun Gupta — January 24, 2008 @ 4:41 am

  4. Siggi, NetBeans project is now available at:


    Comment by Arun Gupta — January 24, 2008 @ 5:53 am

  5. Thank you Arun for this fine piece of work! Have a nice day :-)

    Comment by Siggi — January 24, 2008 @ 6:21 am

  6. Hi Arun very good tutorial. I start to use Jmaki and netbeans to do my web application, but i dont know a hosting provider for glashfish. ¿do you know someone? ¿what need the server for deploy my jmaki web aplication?
    thanks for your help.

    Pd: sorry for my english.

    Comment by javiersinnada — January 24, 2008 @ 1:56 pm

  7. javeirsinnada, Joyent (joyent.com) provides GlassFish hosting. A jMaki application is a WAR file that can be deployed on any Servlet compliant container. Good luck!

    Comment by Arun Gupta — January 24, 2008 @ 3:31 pm

  8. For Web apps RESTFull services combined with RJS and JSON is hard to beat — all the benefits of AJAX/SOAP but much simpler implementation. SOAP based Web services and AJAX still have their place but I am relying on them much less now because of REST/RJS/JSON. Good tutorial and demonstration. Thanks.

    Comment by Jeff — January 28, 2008 @ 12:40 pm

  9. Hello Arun,
    Can you do something similar on Ruby/RoR platform? That would be great.

    Comment by Bharat Ruparel — January 29, 2008 @ 4:51 am

  10. Hi, Arun, i try run this example, but in the step 5. when i Test RESTful Web Services, page open at browser give me a javascript alert with message: "Cannot access WADL: Please restart your RESTful application, and restart this page."

    I don’t know what is wrong.


    Comment by feusebio — February 14, 2008 @ 4:26 am

  11. I have been trying to use RESTful web services for a project but im experincing a lot pf problems with it.

    First of all, is it possible to use server side java scripting with Google maps.
    I basically want to add some customization to the map, and take png images of the modified map and save it at the server. Now the problem i see is that how do I render the html and java script at the server and take a screenshot of it.

    In case you can suggest a better method or a work-around, thanks in advance :)

    Comment by Funkyidol — February 15, 2008 @ 4:20 am

  12. Bharat Ruparel,

    Can you try following url directly on any browser in your machine


    If this url is not accessible (on browser), then the issue is the Glassfish server is not up, or the application sakila did not get deployed properly.

    If the url is accessible, then can you please provide more details (OS, JDK version, browser, Is browser and Glassfish server local,…)


    Comment by Auyb Khan — February 15, 2008 @ 8:26 am

  13. Funkyidol,

    I think you can render Google maps or any javascript on a non-browser environment using javascript engines like XULRunner (See http://wiki.netbeans.org/EmbeddedBrowser for how its may be embedded in a IDE)


    Comment by Ayub Khan — February 15, 2008 @ 8:37 am

  14. Yes, my problem is on deploy to Glassfish:
    "WebModule[/sakila] ServletContext.log():PWC1409: Marking servlet ServletAdaptor as unavailable
    WebModule[/sakila]Error loading WebappClassLoader
    WebModule [sakila] failed to deploy and has been disabled"


    Comment by feusebio — February 15, 2008 @ 8:40 am

  15. Ok, i fixed the problem, i instaled a new version of Netbeans, the new version 6.0.1 with the Glassfish bundled, and now the Rest services run fine.


    Comment by feusebio — February 15, 2008 @ 8:53 am

  16. I have some errors at Toplink connection to sakila database, but i fixed errors with the changes in annotations Film class.
    Where i have:

    "@JoinColumn(name = "language_id", referencedColumnName = "language_id") "

    i put this:

    "@JoinColumn(name = "language_id", referencedColumnName = "language_id", insertable=false, updatable=false)"

    and in this annotation:

    "@JoinColumn(name = "original_language_id",…."

    because the Toplink need one of them read-only column, when i have referenced the column more than one time.

    Comment by feusebio — February 15, 2008 @ 10:24 am

  17. I have a RESTful web application .It has a jsp page in web folder. whenever the jsp page is requested , it is giving this error

    Requested Url : /index.jsp
    org.apache.jasper.JasperException: Unable to compile class for JSP:

    An error occurred at line: 22 in the generated java file
    The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory

    at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
    at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
    at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:654)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:445)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:292)
    at org.iitkgp.erp.service.filter.DigitalSignatureFilter.doFilter(DigitalSignatureFilter.java:65)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)

    pls help…..

    Comment by Somesh Ghosh — May 26, 2008 @ 3:34 am

  18. can a Restful web service and a jsp run on the
    same application context?????
    pls help?

    Comment by Somesh Ghosh — May 26, 2008 @ 3:37 am

  19. Somesh, RESTful Web service can be deployed along with multiple JSP files in the same WAR.

    Have you tried deploying a simple WAR file with JSP and does that work ?

    Are you using GlassFish ?

    If you are using Jersey and facing this problem, then you can post a question to

    Comment by Arun Gupta — May 28, 2008 @ 5:03 pm

  20. Hi Arun,
    I am new to J2EE technology. I need to implement some complex RESTful services using Java technology which will be accessed by millions of clients. I liked your tutorial and have a question. Would you use the mechanism you suggested in your tutorial for some production code or just for learning purpose? Do you have any comments about Restlets?


    Comment by bsis — May 29, 2008 @ 5:12 pm

  21. hi bsis, Jersey is still an early access technology. However, it is pretty stable and in fact, i know of some people who use Jersey for real-life (i.e. non-learning) projects. For instance http://www.smatch.com uses Jersey to provide RESTful API. For further information on Jersey, please go to Jersey main page https://jersey.dev.java.net/ or ask questions at mailing list. You can also try download Jersey snapshot bundle and look at the provided examples. Jersey will become final in September this year, and then it will be also possible to buy official support for it.

    Comment by Jakub — May 30, 2008 @ 2:18 am

  22. Nice tutorial. I did a similar one, http://www.netbeans.org/kb/60/websvc/rest-tomcat.html, on the netbeans 6.1 site using glassfish it worked. When I changed the appserver to tomcat 6, , using mysql 5, I would get the following error: oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerImpl.createQuery(EntityManagerImpl.java:209)

    Is netbeans/tomcat6/mysql 5 /jersey .7.1 compatible?

    Comment by au — July 15, 2008 @ 2:44 pm

  23. Hi Au, Jersey itself does not contain any connector to a db. However, for various reasons, Jersey and also REST Web Services NetBeans plugin, which is based on Jersey, comes bundled with certain libs. I believe here is a space for introducing the "incompatibilities". It would help, if you could report a bug at http://www.netbeans.org/community/issues.html
    providing some steps to reproduce your issue, so that we can track it.

    Comment by Jakub — July 17, 2008 @ 2:06 am

  24. [Trackback] EclipseLink JPA replaces TopLink Essentials as the JPA implementation in GlassFish v3. One of the benefits of using EclipseLink is that it provides efficient pagination support for the MySQL database by generating native SQL statements such as "SELECT …

    Comment by Arun Gupta's Blog — April 8, 2009 @ 5:52 am

  25. [Trackback] I presented on "Creating powerful web applications using GlassFish, MySQL and NetBeans/Eclipse" as the first talk of FISL 10 yesterday. The room was only partial full being the first talk of FISL but got packed towards the middle so…

    Comment by Arun Gupta's Blog — June 24, 2009 @ 8:57 pm

  26. [Trackback] I presented on "Creating powerful web applications using GlassFish, MySQL and NetBeans/Eclipse" as the first talk of FISL 10 yesterday. The room was only partial full being the first talk of FISL but got packed towards the middle so…

    Comment by Arun Gupta's Blog — June 24, 2009 @ 9:01 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress
80361 visits from Sep 11, 2009