Educational

RESTful API – CRUD Operations -Step by Step Guide

Tools and Technologies used 

  1. Database -Oracle (You can use any DB)
  2. Jersey – JAX-RS API 2
  3. SOAP UI for testing
  4. Apache Tomcat Application. (You can use any Application Server)

Methods used in a service class for performing CRUD operations

@POST – Create

@GET – Read

@ PUT -Update

@Delete -Delete

Prerequisites

  1. Knowledge of JAVA Programming language
  2. Application Server basics
  3. DDL basics
  4. Knowledge of Schema languages.

Step 1

Create Entities, Download Resources and Configure.

  • Create an entity in DB on which CRUD operation is going to perform.

Eg.

CREATE TABLE <Your Table Name> (

<Columns>    NUMBER,

<Columns> VARCHAR (20) PRIMARY KEY,

<Columns>   VARCHAR (20),

<Columns>     VARCHAR2(40) NOT NULL,);

  • Download Apache bundle from here, and Jersey bundle from here
  • Configure your apache server connector port to avoid conflict with default port; that might have been used by other programs, to run into your desired port.

\APACHE_HOME\apache-tomcat-7.0.85\conf\server.xml

<Connector port=”9999″ protocol=”HTTP/1.1″ connectionTimeout=”20000″   redirectPort=”8443″ />

  • Write a schema with request and response attributes. Write your elements name exactly same as your DB columns. This way it will be easier to relate when we perform xml to java object conversion

Step 2

Create a simple Dynamic project and run it on the server.

  • Convert XSD schema to java objects. Use xjc tool for conversion, this is quick and easy.
  • Now create a Dynamic Project in Eclipse. Create a package called “generated” and put all the classes generated from XSD in there.
  • Put all the jersey jars (Downloaded in step 1) in lib folder.
  • Configure App to run in Tomcat server. You can do this from eclipse. Go to Windows > Preferences
  • Write a simple index.html – This will be displayed once the server is successfully run.

<!DOCTYPE html>

<html><head><meta charset=”ISO-8859-1″>

<title>Insert title here</title>

</head><body>Welcome to RESTful API </body>

</html>

  • Deploy the project – Right click and Run as >Run on the Server.
  • Your application is running on the server.

 

 

Advertisements