GlassfishDB Example

This example demonstrates how to use JAX-RS with Glassfish and the Java Persistence API to expose the contents of a Derby database table as a RESTful resource.

Contents

The example consists of two Java classes:

com.example.resources.WidgetsResource
A JAX-RS resource class that implements the HTTP GET method and creates a representation by querying the database.
com.example.Persistence.Widget
A Java Persistence API entity class that is used to interact with the database.

Running the Example

The example uses sun-appserv-samples database, which comes along with Java EE 5 Samples GlassFish update center module. In order to install the module you need to run update center client:

$AS_HOME/updatecenter/bin/updatetool
And then install Java EE 5 Samples module from Available Software list.

Then run the example as follows. First start Glassfish:

$AS_HOME/bin/asadmin start-domain
$AS_HOME/bin/asadmin start-database

Build the example via ant, it will create dist/GlassfishDB.war archive.

Deploy GlassfishDB.war using the admin console or via

$AS_HOME/bin/asadmin deploy dist/GlassfishDB.war
and then using a web browser, visit:

http://localhost:8080/GlassfishDB/resources/widgets

If this is the first time running this example you will likely see an empty window. In order to get some content you have to add some rows to the database as follows:

Refresh the web page to see the newly added data:

A WADL description may be accessed at the URL:

http://localhost:8080/GlassfishDB/resources/application.wadl