Developing a REST application using Jersey with JPA and Running it on AWS EC2

Based on what we have built before (see previous post), we are now ready to move further to extend the application to support HTTP GET, POST, PUT and DELETE.

This time we also include JPA for data persistence and use AWS EC2 to deploy the application on Glassfish server.

You can check out the code from Bitbucket.

1. Domain Model

We are building the application to support creating, updating and deleting customer and address information. Each customer can have a list of addresses that belong to it. Each address can only be associated with one customer. Address is uniquely identified by its ID in the database. Each customer is identified by its username.

2. JPA

I chose EclipseLink as JPA implementation. Here is a snippet of persistence.xml file.

	<persistence-unit name="simpleJPA" transaction-type="JTA">
		<jta-data-source>jdbc/MySQL</jta-data-source>
		<class>com.test.jpa.model.Customer</class>
		<class>com.test.jpa.model.Address</class>
	</persistence-unit>

3. Resources

HTTP Method Resource URL Note
GET /customer/{username} get the information about this customer
GET /customer/{username}/address/{addressId} get the information about the address of this customer
GET /customer/{username}/addresses get the information about all the addresses of this customer
POST /customer create a new customer
POST /customer/{username}/address create a new address for this customer
PUT /customer/{username} update the existing customer
PUT /customer/{username}/address/{addressId} update the address of this customer
DELETE /customer/{username} delete this customer and all its addresses
DELETE /customer/{username}/address/{addressId} delete the address of this customer
  • PUT is only implemented to support updating resource.
  • 4. Glassfish & AWS EC2

    At the time of writing, AWS Beanstalk only gives us the option to use Tomcat but not Glassfish if you want to deploy Java application. We will have to install Glassfish on the EC2 instance instead.

    One resource I found very useful is this post. Instead of creating an AWS Beanstalk environment, I chose to create AWS EC2 instance as well as RDS instance and built the environment. Essentially, steps are:

  • Create EC2 and RDS instances
  • Configure security group for both instances
  • Create and configure key pair for EC2
  • For EC2, use Putty to login into the instance as ‘ec2-user’
  • For EC2, update packages, install JDK
  • For EC2, download and unzip Glassfish
  • For Glassfish, set admin user name and password, enable secure login
  • For Glassfish, upload MySQL JDBC connector jar file if you are going to use MySQL
  • For RDS, test database connection
  • For RDS, run the sql scripts to create tables
  • For Glassfish, access admin console
  • For Glassfish, configure JDBC Connection Pool and Resource for MySQL
  • For Glassfish, deploy application
  • 5. Trying out

    Verify application – access time resource

    glassfish.time.json.response

    Create customer

    glassfish.add.customer.response

    Get customer information

    glassfish.get.customer.response

    Create address

    glassfish.add.address.response

    Get customer & address information

    glassfish.get.customer.with.address.response

    Update customer

    glassfish.update.customer.response

    Update address

    glassfish.update.address.response

    Delete customer

    glassfish.delete.customer.response

    6. Final words

  • Not every possible operations on the customer and address are implemented
  • There have been some arguments (see here and here) that exposing domain model in the REST response is not ideal (anti-pattern) – I agree but what is implemented in this application still does so for the sake of simplifying.
  • Advertisements
    Developing a REST application using Jersey with JPA and Running it on AWS EC2

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s