RESTful representation of "sakila" using GlassFish and NetBeans IDE

"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

Comments:

Is this Jersey 0.5 or 0.6?

Posted by Alexis MP on January 23, 2008 at 04:55 PM PST #

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

Posted by Siggi on January 23, 2008 at 05:03 PM PST #

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.

Posted by Arun Gupta on January 23, 2008 at 08:41 PM PST #

Siggi, NetBeans project is now available at:

http://blogs.sun.com/arungupta/resource/sakila-glassfish-netbeans-project.zip

Posted by Arun Gupta on January 23, 2008 at 09:53 PM PST #

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

Posted by Siggi on January 23, 2008 at 10:21 PM PST #

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.

Posted by javiersinnada on January 24, 2008 at 05:56 AM PST #

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!

Posted by Arun Gupta on January 24, 2008 at 07:31 AM PST #

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.

Posted by Jeff on January 28, 2008 at 04:40 AM PST #

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

Posted by Bharat Ruparel on January 28, 2008 at 08:51 PM PST #

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.

Thanks.

Posted by feusebio on February 13, 2008 at 08:26 PM PST #

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

Posted by Funkyidol on February 14, 2008 at 08:20 PM PST #

Bharat Ruparel,

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

http://localhost:8080/sakila/resources/films/

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

Thanks
Ayub

Posted by Auyb Khan on February 15, 2008 at 12:26 AM PST #

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)

Thanks
Ayub

Posted by Ayub Khan on February 15, 2008 at 12:37 AM PST #

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

Thanks

Posted by feusebio on February 15, 2008 at 12:40 AM PST #

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.

Thanks.

Posted by feusebio on February 15, 2008 at 12:53 AM PST #

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.

Posted by feusebio on February 15, 2008 at 02:24 AM PST #

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

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

Posted by Somesh Ghosh on May 25, 2008 at 08:34 PM PDT #

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

Posted by Somesh Ghosh on May 25, 2008 at 08:37 PM PDT #

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

Posted by Arun Gupta on May 28, 2008 at 10:03 AM PDT #

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?

Thanks
bsis

Posted by bsis on May 29, 2008 at 10:12 AM PDT #

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.

Posted by Jakub on May 29, 2008 at 07:18 PM PDT #

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?

Posted by au on July 15, 2008 at 07:44 AM PDT #

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.

Posted by Jakub on July 16, 2008 at 07:06 PM PDT #

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

Posted by Arun Gupta's Blog on April 07, 2009 at 10:52 PM PDT #

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

Posted by Arun Gupta's Blog on June 24, 2009 at 01:57 PM PDT #

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

Posted by Arun Gupta's Blog on June 24, 2009 at 02:01 PM PDT #

hi facing problem to access glassfish application as web applicastiom

Posted by prahant on September 11, 2009 at 02:31 AM PDT #

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

Posted by Arun Gupta on September 11, 2009 at 02:56 AM PDT #

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.

Posted by Stijn on February 06, 2010 at 07:20 PM PST #

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.

Posted by kawee on November 24, 2010 at 06:48 PM PST #

kawee,

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:

http://netbeans.org/bugzilla/show_bug.cgi?id=192733

Posted by Arun Gupta on November 30, 2010 at 07:47 PM PST #

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.

Posted by guest on May 10, 2011 at 02:07 PM PDT #

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.

Posted by Arun Gupta on May 16, 2011 at 02:43 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today