DBA > Articles

RESTful WS with Jersey, Part 2

By: Manoj Debnath
To read more DBA articles, visit http://dba.fyicenter.com/article/

This article further takes Part 1 of this article series to the realm of RESTful Web Services with Jersey. It contains almost no theoretical/conceptual background and delves directly into implementing one. Programmers often find implementing RESTful CRUD implementations that use EJB and ORM tools a daunting task. Here, I have tried to depict in pictorial detail the steps needed to create one. So, without further ado, let's get our hands dirty.

Some Prerequisites

We'll explore RESTful Web services with EJB and an ORM tool such an EclipseLink to represent data persistence. Because Tomcat 7/8 is not JEE compliant, we shall be using Glassfish 4.1 as the Application Server. The backend database would be MySQL with a JDBC type 4 driver. And, for all the required jar files, we shall depend on Maven (pom.xml). To sum up, the requirements list is as follows:
Java SDK 8
Netbeans 8.0.2 (bundled with Glassfish 4.1 + all we need for the project)
MySQL 5


A file named pom.xml will be created automatically; it contains a list of dependent jars. Maven will download a list of dependent jars behind the scenes. We need to add a few more dependencies to use EclipseLink and EJB in our project. So, the exact pom.xml file will as follows.


<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>org.mano.bookstore</groupId>
<artifactId>bookstorerestapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>bookstorerestapp</name>

<build>
<finalName>bookstorerestapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>

</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need -->
<!-- servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId --> </dependency>

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.
modelgen.processor</artifactId>
<version>2.5.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.2_spec</artifactId>
<version>1.0.0.Final</version>
<type>jar</type>
</dependency>
</dependencies>
<properties>
<jersey.version>2.21.1</jersey.version>
<project.build.sourceEncoding>UTF-8
</project.build.sourceEncoding>
</properties>
</project>


1. Creating a Maven Project from the List of Archetypes
Open the NetBeans IDE. Create a New Project from the File menu.
2. Table Structure for a MySQL Database
Create a rudimentary database and table SQL DDL. For more information on how to create a database and tables in MySQL, refer to the MySQL documentation.

CREATE DATABASE bookstore2;

CREATE TABLE books (
ID INT NOT NULL,
COPYRIGHT VARCHAR(255),
EDITIONNUMBER INT,
ISBN VARCHAR(255),
PRICE DOUBLE,
PUBLISHER VARCHAR(255),
TITLE VARCHAR(255),
PRIMARY KEY (ID)
) ENGINE=InnoDB;


3. Creating a Connection Pool and JNDI Resource in Glassfish
A Java EE application grants access to databases through the JDBC API. Creating a connection pool means that the data source is registered with the application server for use by the application running within the container. There may more than one data source available; hence the name connection pool. JNDI provides the name through which the JEE application gets hold of the data source in the container at runtime.

First, create a connection pool for the MySQL datasource as follows:s
Start Glassfish server from NetBeans.
Open the Administrative Console in the browser with the URL
http://localhost:4848/common/index.jsf.
Select JDBC, JDBC Connection Pools. and New….

Full article...


Other Related Articles

... to read more DBA articles, visit http://dba.fyicenter.com/article/