Spring Boot – Build a REST API

Building a REST API in Spring Boot

In this tutorial, we would would outline the steps to Build a REST API. We would also plan the various components of out REST API.

As a reminder, an API(Application Programming Interface) allows exposes functionality of an application to other applications. So as a programmer, you’ll spend most of your time building APIs.

 

  1. About the API we’ll Build
  2. Create the User’s Class
  3. Create the Location Class
  4. Create the Post Class
  5. Watch the Video

 

1. About the API we’ll Build

So we would build an API for a social network platform. Let’s call it SocialAPI. This is like a mini Facebook! Although it is small, this is the same principle social networks follow.

 

Our SocialAPI would contain the following classes:

  • list of users
  • list of locations
  • list of posts

 

Also, the following relationships would be supported:

  • a user has a location
  • each location could have 1 or more users
  • a user could create a post. Hence each user can have one or more posts

 

Our SocialAPI would support the following operations:

  • add, delete and update user details
  • add, delete and update location details
  • add, delete and update post details
  • get a list of users or locations
  • get user or location by id
  • get list of posts for a particular user

 

The entity relationship diagram for our SocialAPI is shown below.

 

Database Diagram for our Social API

 

2. Create the Users Class

  • First you need to create a package that would contain the User class
  • Then, inside package create a class and call it user
  • The content of the User class is shown below. So you can copy and paste. You can also type it out yourself

 

package user;

public class User {
    private String id;
    private String firstname;
    private String lastname;
    private Location location;
    private String email;
		
}

After you have added this code, then you can generate the getters and setters.

 

3. Create the Post Class

  • First you need to create a package that would contain the Post class
  • Then, inside the course package create a class and call it Post
  • The content of the Post class is shown below. So you can copy and paste. You can also type it out yourself

 

package post;

import user.User;

public class Post {
   private String id;
   private String postdate;
   private User user;
   private String details;
}

Again, remember to generate the getters and setters for this the User class

 

4. Create the Location Class

It follows the same process

  • First you need to create a package that would contain the Location class
  • Then inside the location package create a class and call it Location
  • The content of the Location class is shown below. So you can copy and paste. You can also type it out yourself

 

package location;

public class Location {
   private String id;
   private String name;
}

Also remember to generate getters and setters. You can watch the video below to see how you can do this.

 

So at this point, you have successfully set up the structure of the SocialAPI. So in the next lesson, we would add service to get list of items.