Introduction

This article shows how to make a simple, end-to-end, mobile enterprise application for developers who are just learning how to make enterprise applications or are learning how to use a combination of the components described here in an application. Our example includes a RESTful web service that provides access to Java entity beans on Oracle’s
Glassfish application server and a simple Android client to consume it.

Serialization of the entity beans between Android and Glassfish is encoded
through JSON. All code is in Java unless otherwise specified.

Background

Earlier
this year (2012), during a student assignment at Humber College in Etobicoke,
Ontario, Canada, I was suddenly struck by the clever idea that one could
simultaneously demonstrate the consumption of our RESTful web services with an
Android application and the serialization of Java entity beans through JSON. It
was only a few weeks later that my Java instructor informed me to my dismay
that this was not an original idea even with his own past classes. Nonetheless,
I did not find another article on the web that duplicates all that this one
does (although my search may not have been thorough enough); therefore, I bestow
the precious insight herein, gleaned by the sweat of my brow, as a warm light through
all mankind to share. (Self-deprecating, Avengers-inspired sarcasm intended.)

The code shown
has been extracted from my assignment and simplified for this presentation.

Using the code

We assume that our audience knows how to make simple examples of a database, an EJB-hosted RESTful web service, and an Android Activity. However, one should be able to get the gist of the code with just a little programming experience.

For the NetBeans web service project, I added the free, current, jettison.jar from jettison.codehaus.org. It includes the JSON STaX implementation, which contains the BadgerFish mapping of JSON to XML.

Data Model

Database

The underlying database table has just enough fields for the purposes of this article: an identifier, a
label, and an info field to update. Here is the MySQL DDL script snippet that describes it:

Business Logic

The RESTful
web service provides the basic CRUD access operations to the entity beans,
which are the resources in the REST paradigm: create, retrieve, update, delete.

Glassfish
includes Jersey, the Reference Implementation for building RESTful web services
in Java. We also add the JSON/XML mapping provided by BadgerFish in the
Jettison library available at jettison.codehaus.org.

Our web
service is implemented by a stateless Enterprise JavaBean, SimpleUserResource.
We have also set the REST ApplicationPath attribute to be “rest”.

Create

Our
adopted REST paradigm calls for resources to be created by the HTTP request
POST. This
snippet includes the start of our implementation class.

The @Path
attribute specifies the URL path component that indicates to Glassfish that
containing HTTP requests should be directed to this EJB.

The @Consumes
and @Produces attributes specify the acceptable MIME types of the content of
the HTTP requests that are received and responses that are sent, respectively.
(WILDCARD indicates all types, of course.) An attribute specified for a method
overrides a corresponding one for its class.

The JAXBElement
parameters wraps the posted entity bean after BadgerFish has converted the
bean’s HTTP JSON representation to XML and Project JAXB has converted the XML
representation to a SimpleUser class instance.

Retrieval

Our
adopted REST paradigm calls for resources to be retrieved by the HTTP request
GET.

There are
two basic types of retrieval. One is to retrieve a specific resource.

The {id} template in @Path specifies that an
URL in
a HTTP GET request with an extra path component after “appuser” indicates that
retrieveSimpleUser() will be invoked to handle the request with the text of
that extra component being passed as an integer in the parameter named id.

The returned SimpleUser instance will be converted
to XML by JAXB and JSON by BadgerFish before being sent to the client in the
resulting HTTP response.

Another type of retrieval is to fetch the list of
resources located at a specific directory.

The {id} template in @Path works the same way as
for retrieveSimpleUser().

Android Client

Now, brace yourself for the stunning beauty of this
Android client application.

Displayed in this UI is
the entity bean with id=3, name=”Dustin Penner”, and email="dustin.penner@simpleuser.org".

Now, a consideration in
building this app is how to serialize the bean. A reflexive instinct of the
typical contemporary software developer, trained to use application frameworks,
is to reach for a library off the internet that will handle the serialization
to JSON automatically, e.g. Jackson at jackson.codehaus.org. You know the
thinking – don’t reinvent the wheel; don’t create errors by, heaven forbid, programming
your own code; outsource the responsibility to the library maker. However, I
think that, in this case, adding a library to the client is overkill. The
SimpleUser entity is *simple* - we can handle it. We can include our own
conversion methods in the entity class, which needs to be imported as a library
into the Android client project anyway. We don’t have to be burdened with the
overhead of including and updating the extra library in multitudes of client apps.
And, for the purpose of enlightenment, we also get to show JSON serialization in
action in this article.

Let’s create an entity to
represent the great, young Canadian hockey defenseman, Drew Doughty. The method in the client sends
out a HTTP POST request over a raw HttpURLConnection as follows:

Thus, when an Android-wielding
hockey fan wants to record forever the name and email of a certain slick-skating
L.A. King, his press of the Create User button will send a textual request
like the following, bypassing any typical firewalls, directly to our web
service: