NetBeans in the Classroom: Teaching RESTful Web Services
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.