NetBeans in the Classroom: Teaching RESTful Web Services
Join the DZone community and get the full member experience.Join For Free
in preparing for a second class i am about to give on restful web services, i decided to show my students the netbeans wizard "
restful web services from database"
. there is a tutorial on this called
getting started with restful web services
at this location:
i started up netbeans ide 8.0 and proceeded to follow the instructions.
first, here is my working environment:
the first thing the tutorial instructs you to do is to create a java web – web application.
after creating the project you need to create the entity and web services class using the restful web services from database wizard.
the next screen requires you to select the data source. the tutorial assumes that you have a mysql database called ‘sample’ not to be confused with the derby database called ‘sample’. i did not have this database but my plan was to use a database that i had used in previous courses called ‘aquarium’. this is a single table database listing details about fish for aquariums.
i already had a netbeans connection to this database so i followed the tutorial but used my aquarium database instead of samples.
so far so good. with the data source created, i was able to select the single table called ‘fish’. on the next dialog i entered a package name. a quick tip, always place entity classes in packages that have as the last name ‘entities’. another tip, on the last dialog you are presented with the package name for the services. in my case it wanted ‘com.kenfogel.entities.service’ that i shortened to ‘com.kenfogel.service’. i clicked on finish.
the wizard proceeds to generate all the entity classes and the restful classes. very cool. without writing a single line of code this is what i had.
the next step in the tutorial is about testing the web service. you need to create another java web – web application project. once created you select your original project and right mouse click to reveal test restful web service . the configure rest test client dialog appears and you are told to select web test client in project . browse to your new project and the dialog should look something like:
at this point it started to go wrong. clicking ok results in a number of files being added to the webservicestest project. glassfish is started and the browser opens here:
...and presents a 404 error. the problem is that this page is really at this location:
correcting the url in the browser gives you:
returning to netbeans i looked at the glassfish server 4 output window. there was a stack trace. quite a long stack trace. after reviewing the trace i zeroed in on the following message:
severe: exception while preparing the app : invalid resource : jdbc/aquarium__pm
well now i’m stymied. i followed the tutorial almost exactly except for using my database. when the web services application deployed it rejected the database resource. more confusing is that it rejected a resource with a different name. mine was called jdbc/aquarium but the error called it aquarium__pm.
when i started out as a freelance programmer i used to add as much as a thousand dollars to my job estimates to cover the cost of books i was going to need to learn what i just told a client i could do. today all you have to do fire up your favourite search engine and you are awash in thousands of links. in this case i discovered that other programmers had encountered a similar problem. the solution, i learned after a few false starts, was to register a resource and a pool directly in glassfish. i found a tutorial titled ‘how to setup a jdbc connection in glassfish’ at this location:
i followed the instructions and found that the jndi name i used in glassfish now appeared in the list of available data sources when i ran the restful web services from database wizard in netbeans. but when i chose the name an error message appeared in netbeans.
some people had left comments on this tutorial about this error. the author’s response was that he did not use netbeans and since it worked for him it was a problem with netbeans. so back to the search engine,
here i discovered that indeed some people reported this problem as a bug in netbeans. you can see the bug report at this location:
well, it appears to me that there is no bug. the last person to comment (ahead of the comment i just added) had the solution. you just have to add a property to the glassfish jdbc connection pool called ‘driverclass’ and give it the value of ‘com.mysql.jdbc.driver‘. not a bug but a failure in the documentation.
here are the properties if your jdbc connection pool is using java.sql.driver for the resource type.
if you plan to use the one of the other choices you will have to make similar changes. for example, if you use java.sql.datasource there are 214 properties. going through this list you will see where to put the url, user and password. the datasource is not required because it is already a property.
in working through this problem i recreated the work every time i thought i found a solution. i must have created the web projects at least two dozen times. the end result is a repeatable solution in the environment i use. in the end this is what the test should look like if you choose the url for count.
if you are interested in the related powerpoint presentation, see the slidedeck below. the presentation, 16 slides, goes step by step on how to configure glassfish for a mysql jdbc connection pool and jdbc resource.
Opinions expressed by DZone contributors are their own.