Miles to go ...

Arun Gupta is a Technology Evangelist for Web Services and Web 2.0 Apps at Sun. He was the spec lead for APIs in the Java platform, committer in multiple Open Source projects, participated in standard bodies and contributed to Java EE and SE releases.
« FREE chance to win... | Main | Little Rock National... »

http://blogs.sun.com/arungupta/date/20080124 Thursday January 24, 2008

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

del.icio.us | furl | simpy | slashdot | technorati | digg
Comments:

Is this Jersey 0.5 or 0.6?

Posted by Alexis MP on January 24, 2008 at 12:55 AM 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 24, 2008 at 01:03 AM 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 24, 2008 at 04:41 AM 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 24, 2008 at 05:53 AM PST #

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

Posted by Siggi on January 24, 2008 at 06:21 AM 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 01:56 PM 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 03:31 PM 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 12:40 PM PST #

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

Posted by Bharat Ruparel on January 29, 2008 at 04:51 AM 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 14, 2008 at 04:26 AM 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 15, 2008 at 04:20 AM 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 08: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 08: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 08: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 08: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 10: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 26, 2008 at 03:34 AM PDT #

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

Posted by Somesh Ghosh on May 26, 2008 at 03:37 AM 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 05:03 PM 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 05:12 PM 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 30, 2008 at 02:18 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
« FREE chance to win... | Main | Little Rock National... »

Valid HTML! Valid CSS!

This is a personal weblog, I do not speak for my employer.

--> ajax ajaxworld conf eclipse fitness gem glassfish glassfishday hyderabad india indigo interoperability javaone javaone2008 jax-ws jmaki jpa jruby mac marathon metro microsoft mysql netbeans phobos photography presos railsconf ruby rubyonrails running runninglog runsfm screencast siliconvalleymarathon sun suntechdays swdp tango theserverside totd training traveltips v3 vista wcf web2.0 webservices webtier windows wsaddressing wsit
Locations of visitors to this page

calendar

« July 2008
SunMonTueWedThuFriSat
  
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
31
  
       
Today

Stats

Today's Page Hits: 5651


Total # blog entries: 629
Total # comments: 1784

www.flickr.com
This is a Flickr badge showing public photos from ArunGupta. Make your own badge here.
Add to Technorati Favorites

Last 50