REST WEBSERVICE

Designing the REST API: Registration Application

Now whenever we talk about the REST Web services, there are two terms that should always come to your mind - first is, either you are exposing the web service (acting as server) or you will be consuming the web service (acting as client).

Exposing a web service means that you have made the web service and has deployed it on your server and this can be consumed by any client by using the URL to our service.

Let us make a simple REST API and also understand the design aspects of making the REST API. In this tutorial we will build a simple Registration API. It will have students, classes and the information of students enrolled in different classes.

So first, let's summarise what this registration app can do:

  1. It can insert a new record of the student.
  2. It can get the record of the student.
  3. It can list all the courses which are available.
  4. It can get the information of all the courses.
  5. It can be used to enroll for a course by a particular student.
  6. It can be used to get all the courses enrolled by any particular student.
  7. It can be used to get all the students who have enrolled for any particular course.

Now let's start designing the application. The first thing we should do is designing the Database Model.

Designing the REST API

ER diagram of the Registration App.


If we look at the ER Diagram of the Registration app, the basic entities are:

  1. Students Table: This is responsible for storing the data of all the students. Student ID will be the primary key.
  2. Courses: This table will have the information of all the courses taught in the school. Course ID will be the primary key.
  3. Registrations: This table will maintain the registration information of all the registrations. Student ID along with the Course ID will be the primary key for this table.

Now let's define the URI for our application's various services. Notice that in the registration application everything will be URI.

/Myapp/students/

This will give information about all the students.

/Myapp/courses/ 

This will give information about all the courses.

/Myapp/registrations/

This will give information about all the registrations data.

/Myapp/students/1/registrations

This will give information about the registration of student with roll no 1.

Notice two things about the URI:

  1. The URI contains nouns not verbs. In our system - students, courses, registrations are resources. You do not have URI like getStudents or getCourses.
  2. Secondly, the resources names should be plural in every case.

The advantage of such resource based URI is that they are not dependent on the framework you use.


Now the URI for student with the student ID 1 will be /Myapp/students/1 and for the student ID 2 it will be /Myapp/students/2.

Similarly with the courses we can have resource based URI. For example: for course with ID 1 it will be, /Myapp/courses/1/

To get the registrations for a particular student we will use the query parameter, which we will discuss in the upcoming lessons.