Pages

  • Home
  • Contact Me
spacer

August 18, 2010

Creating a RESTful Web Service - Part 1/5


I have been doing a lot of work with data access services recently so I figure it's time to share what I have discovered.  Don't be scared off that this series is broken into 5 parts, I will keep them short:
  • Part 1 - The Database
  • Part 2 - Mapping the Database to JPA Entities
  • Part 3 - Mapping JPA entities to XML (using JAXB)
  • Part 4 - The RESTful Service
  • Part 5 - The Client

In this series of posts we will use a number of standard Java EE technologies to quickly create a RESTful data access service:
  • JSR-317 - Java Persistence Architecture (JPA)
  • JSR-222 - Java Architecture for XML Binding (JAXB)
  • JSR-220 - Enterprise JavaBeans (EJB)
  • JSR-311 - The Java API for RESTful Web Services (JAX-RS)


Database Model

The following database model will be used for this example.  This particular database stores customer related information.  I am using Oracle Database XE, but you could use almost any database with a JDBC driver.

CUSTOMER Table

CREATE TABLE "CUSTOMER" 
 ( "ID" NUMBER NOT NULL ENABLE, 
 "FIRST_NAME" VARCHAR2(50), 
 "LAST_NAME" VARCHAR2(50), 
 CONSTRAINT "CUSTOMER_PK" PRIMARY KEY ("ID") ENABLE
 )
/

ADDRESS Table

CREATE TABLE "ADDRESS" 
 ( "ID" NUMBER NOT NULL ENABLE, 
 "STREET" VARCHAR2(50), 
 "CITY" VARCHAR2(50), 
 CONSTRAINT "ADDRESS_PK" PRIMARY KEY ("ID") ENABLE, 
 CONSTRAINT "ADDRESS_FK" FOREIGN KEY ("ID")
 REFERENCES "CUSTOMER" ("ID") ENABLE
 )
/

PHONE_NUMBER Table

CREATE TABLE "PHONE_NUMBER" 
 ( "ID" NUMBER NOT NULL ENABLE, 
 "TYPE" VARCHAR2(50), 
 "NUM" VARCHAR2(50), 
 "ID_CUSTOMER" NUMBER, 
 CONSTRAINT "PHONE_NUMBER_PK" PRIMARY KEY ("ID") ENABLE, 
 CONSTRAINT "PHONE_NUMBER_FK" FOREIGN KEY ("ID_CUSTOMER")
 REFERENCES "CUSTOMER" ("ID") ENABLE
 )
/

JDBC Resource & Connection Pool

Next we need to configure a connection pool on our application server.  I will be using GlassFish Server Open Source Edition version 3.0.1.  These steps will vary depending on the application server you are using. 

  1. Copy the JDBC driver (ojdbc14.jar) to <glassfish_home>/glashfish/lib
  2. Launch the Administration Console
  3. Create a Connection Pool:
    1. Name = CustomerService
    2. Resource Type = 'javax.sql.ConnectionPoolDataSource'
    3. Database Vendor = Oracle (or whatever database vendor is appropriate to your database)
    4. Click Next and fill in the following "Additional Properties":
      1. User (e.g. CustomerService)
      2. Password (e.g. password)
      3. URL (e.g. jdbc:oracle:thin:@localhost:1521:XE)
    5. Use the "Ping" button to test your database connection
  4. Create a JDBC Resource called "CustomerService"
    1. JNDI Name = CustomerService
    2. Pool Name = CustomerService (name of the connection pool you created in the previous step)

Next Steps

In the next post we will examine how to use the Java Persistence Architecture (JPA) to expose the database data as entities (POJOs).
  • Part 1 - The Database
  • Part 2 - Mapping the Database to JPA Entities
  • Part 3 - Mapping JPA entities to XML (using JAXB)
  • Part 4 - The RESTful Service
  • Part 5 - The Client

Further Reading
 
If you enjoyed this post you may also be interested in:
  • RESTful Services
    • MOXy's XML Metadata in a JAX-RS Service
    • MOXy as Your JAX-RS JSON Provider - Server Side  
    • MOXy as Your JAX-RS JSON Provider - Client Side
  •  Application Server Integration
    • GlassFish 3.1.2 is Full of MOXy (EclipseLink JAXB)
    • EclipseLink MOXy is the JAXB Provider in WebLogic Server 12c

3 comments:

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.