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!
sakila".mysqld-nt --user root
--console' in bin directory on Windows or './bin/mysqld_safe'
from MySQL directory on Unix flavors.sakila".
Choose "GlassFish v2" as the "Server:".Services" tab of NetBeans IDE, expand "Drivers" and add
MySQL
Connector/J driver if it does not exist already.
New", "Entity Classes from
Database...". In "Data Source", select "New Data Source..." and specify the
values as shown below:
film" in "Available Tables" and click on "Add >" as shown
below:
Next >".Create Persistence Unit..." and take all the defaults as shown
below:
Create".sakila" as shown below:
Finish".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>Tools", "Plugins", "Available Plugins", "RESTful
Web Services" and then click on "Install". This installs the
Jersey Tooling
Plugin in the IDE.New", "RESTful Web Services from
Entity Classes...".Add >>", take all other defaults as shown
below:
Next >", take all defaults and then "Finish".Test RESTful Web Services".
The following web page is presented in the browser:
films" and then on "Test" as shown
below:
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.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();
}

Here are few more ideas for you to explore:
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
Posted by Arun Gupta in webservices | Comments[21]
|
|
|
|
Today's Page Hits: 5651
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 #