RESTful representation of "sakila" using GlassFish and NetBeans IDE

Guest Author

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

Join the discussion

Comments ( 33 )
  • Alexis MP Thursday, January 24, 2008

    Is this Jersey 0.5 or 0.6?

  • Siggi Thursday, January 24, 2008

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

  • Arun Gupta Thursday, January 24, 2008

    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.

  • Arun Gupta Thursday, January 24, 2008
  • Siggi Thursday, January 24, 2008

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

  • javiersinnada Thursday, January 24, 2008

    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.

  • Arun Gupta Thursday, January 24, 2008

    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!

  • Jeff Monday, January 28, 2008

    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.

  • Bharat Ruparel Tuesday, January 29, 2008

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

  • feusebio Thursday, February 14, 2008

    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.


  • Funkyidol Friday, February 15, 2008

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

  • Auyb Khan Friday, February 15, 2008

    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,...)


  • Ayub Khan Friday, February 15, 2008


    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)


  • feusebio Friday, February 15, 2008

    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"


  • feusebio Friday, February 15, 2008

    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.


  • feusebio Friday, February 15, 2008

    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.

  • Somesh Ghosh Monday, May 26, 2008

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

  • Somesh Ghosh Monday, May 26, 2008

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

  • Arun Gupta Wednesday, May 28, 2008

    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 users@jersey.dev.java.net

  • bsis Thursday, May 29, 2008

    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?


  • Jakub Friday, May 30, 2008

    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 users@jersey.dev.java.net 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.

  • au Tuesday, July 15, 2008

    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?

  • Jakub Thursday, July 17, 2008

    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.

  • Arun Gupta's Blog Wednesday, April 8, 2009
    [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 ...
  • Arun Gupta's Blog Wednesday, June 24, 2009
    [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...
  • Arun Gupta's Blog Wednesday, June 24, 2009
    [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...
  • prahant Friday, September 11, 2009

    hi facing problem to access glassfish application as web applicastiom

  • Arun Gupta Friday, September 11, 2009

    Please post your question to users@glassfish.dev.java.net with complete details.

  • Stijn Sunday, February 7, 2010

    Nice tutorial. Glassfish + EclipseLink seems to become a very nice platform for JPA. Too bad you used Netbeans and not Eclipse, but that's just my personal preference.

  • kawee Thursday, November 25, 2010

    Hello Arun

    What server do you use (tomcat 6.0 or glasfish)
    I have an error like this

    "Help Page

    Cannot access WADL: Please restart your REST application, and refresh this page.

    If you still see this error and if you are accessing this page using Firefox with Firebug plugin, then
    you need to disable firebug for local files. That is from Firefox menubar, check
    Tools > Firebug > Disable Firebug for Local Files"

    I use Apache tomcat 6.0.26
    Please help me.
    Thank you.

  • Arun Gupta Wednesday, December 1, 2010


    I used GlassFish.

    What version of NetBeans are you using ?

    I found application.wadl not generating on 7.0 Beta and filed a bug for the same at:


  • guest Tuesday, May 10, 2011
    Hi Arun, I would appreciate if you could tell me the steps for converting or generating the WSDL file from WADL file. My restful web services generated the WADL file using JERSEY api, but my client needs wsdl file for the same. As i know that restful web services supports WSDL 2.0. But still i am not able to generate wsdl file from WADL/ I am using Eclipse ide, Please respond at the earliest.
  • Arun Gupta Monday, May 16, 2011
    guest, WSDL is generated for a SOAP-based Web service and WADL is generated for a RESTful Web service. If you want to generate WSDL for your endpoint then you'll need to use @WebService on your POJO and then the WSDL will be generated at runtime for you. You can also look at several screencasts at: http://blogs.oracle.com/arungupta/tags/screencast+webservices that explains the above in detail.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.