This example demonstrates how to use JPA in the backend. The example is based on bookmark example from the RESTful Web Services book, which was inspired by the http://delicious.com/ web service.
A bookmark web application is presented that is capable of maintaining users and their bookmarks.
The example consists of four web resources implemented by the following:
com.sun.jersey.samples.bookmark.resources.UsersResource
com.sun.jersey.samples.bookmark.resources.UserResource
com.sun.jersey.samples.bookmark.resources.BookmarksResource
com.sun.jersey.samples.bookmark.resources.BookmarkResource
The mapping of the URI path space is presented in the following table:
URI path | Resource class | HTTP methods |
---|---|---|
/users/ | UsersResource | GET |
/users/{userid} | UserResource | GET, PUT (used also for creating new users), DELETE |
/users/{userid}/bookmarks | BookmarksResource | GET, POST |
/users/{userid}/bookmarks/{bmid} | BookmarkResource | GET, PUT, DELETE |
Bookmark example runs on Glassfish 3.x application servers (http://glassfish.java.net) and needs a running JavaDB (http://www.oracle.com/technetwork/java/javadb/overview/index.html) instance for underlying data (it comes along with GlassFish).
Presuming, you have installed Glassfish 3.1 AS_HOME
variable should point to
your glassfish installation directory.
If .asadminpass
file is missing at your home directory, you
will need to run the following command to get it created:
$AS_HOME/bin/asadmin start-domain $AS_HOME/bin/asadmin login
You then build and run the example by
mvn package $AS_HOME/bin/asadmin start-domain $AS_HOME/bin/asadmin start-database $AS_HOME/bin/asadmin create-jdbc-connection-pool \ --datasourceclassname org.apache.derby.jdbc.ClientDataSource \ --restype javax.sql.DataSource \ --property "portnumber=1527:password=REST:user=REST:serverName=localhost:databaseName=BookmarkDB:connectionAttributes=;create\=true" bookmarkPool $AS_HOME/bin/asadmin create-jdbc-resource --connectionpoolid bookmarkPool jdbc/bookmarkSample $AS_HOME/bin/asadmin deploy target/bookmark.war
Notice: On Windows: you might need to replace asadmin with asadmin.bat. Also, because it seems there are some issues with copy-pasting backslashes, you will also need to use the following one-liner instead of the above mentioned complex command:
asadmin create-jdbc-connection-pool --datasourceclassname org.apache.derby.jdbc.ClientDataSource --restype javax.sql.DataSource --property "portnumber=1527:password=REST:user=REST:serverName=localhost:databaseName=BookmarkDB:connectionAttributes=;create\=true" bookmarkPooland make sure you do not miss the backslash (\) in
create\=true
part.
mvn test -DargLine="-Djersey.test.containerFactory=com.sun.jersey.test.framework.spi.container.external.ExternalTestContainerFactory -DenableLogging -Djersey.test.port=8080" -Ptest