Let’s Build Backend for Friends in Our Social Network App using Spring

Nil Madhab
Apr 7 · 4 min read

In this tutorial, we are going to add a Friend feature using Springboot Backend for a demo social network site we are building

In this tutorial, we will extend the existing app into a social network site, like facebook, where users can add each other friend and have real time chat and video/voice call.

We will be building the backend of the friend feature, in this tutorial

Final Result

You can play with the app here

Friends Feature

API summery

We will be going to do something like this.

For this we are going to need 2 APIs :

1. To add a Friend (addFriend)

2. To List Friends (listFriends)

Let’s Get Started.

Pre-requisites

  1. Knowledge of Java, OOP & Spring Boot Framework
  2. Java Development Kit (JDK)
  3. IntelliJ IDEA Ultimate — open-source (Recommended)
  4. MySQL/MariaDB Database
  5. A good browser (Chrome — recommended)

Flow

  1. Create a model to store Friends.
  2. Create a Friends Repository to interact with the Friends model.
  3. Create Services to perform CRUD operations.
  4. Create API endpoints to interact with Frontend.

Model (Entity)

The model describes that how the SQL Table will get structured by Spring boot.
we can add the Table Name, Column Name, and Key related fields using our model class.

We will start by creating a Model class for Friends Friend.java .

We have added 3 fields to the model user id of 2 users who are wanted to be friends and the date of record created. The date of record created can be used later as we show the friends since x months.

Now, that we have created our model, we need to create a Repository to create and apply various methods in which we can find records by specifying particular conditions.

Repository

If you are familiar with Spring Boot, you would know that Repository the interface contains methods to fetch data from the table.

Creating CRUD methods manually means writing a lot of boilerplate code unless you let the JPARepository interface carry about routine.

Following is the complete code of FriendRepositoy.java

  • Extending JPARepository will automatically create and implement methods for the basic CRUD operations.
  • we have defined 3 methods for the repository.
  1. existsByFirstUserAndSecondUser:- This method gets 2 User as an input and will send the true if the matching record exists.
  2. findByFirstUser:- This method will take a User object and will return a List of records which consist of the specified user as the first user in Table.
  3. findBySecondUser:- This method will take the User object and will return a list of records which consist of the specified user as a second user in the table.

Now, our Friend Repository looks good to go.

Let’s go towards the services we will need to create for the friends' feature.

Service

services are the essential part of Creating RESTful web services. services define the methods of operations that will be triggering on API calls.(GET ,POST)

Here’s the complete code for Service FriendService.java.

Currently, we need to add two services to our feature.

  1. To add friend:- saveFriend the method will store the friends' user in the Friends table in the database.
  2. To list all friend:- getFriends the method will return the list of all friends of the current Logged-in User.

That’s all for Service.

Controller

Now, Let’s begin with writing code for the controller. If you are familiar with Spring Boot or any other MVC framework, you would already know that controller defines the endpoints of the API.

Create a file FriendController.java and paste the following code.

Here GetMapping represents the Api Method GET and String passed in Constructor of GetMapping represents the API Endpoint for getting method.

  1. addUser:- This method will accept the user id of another user with whom the logged-in user wants to be a Friend.
  2. get friends:- This method will return the List of Users which are Friends with the current Logged In User.

Note, that these methods Use the services to perform various operations like adding/fetching/modifying records from/to database.

Congratulations!!!

Congratulations, we have now added the Friends feature to our backend. We will build the front end in the next tutorial.

Javarevisited

Medium’s largest Java publication, followed by 10000+ programmers. Follow to join our community.

Sign up for Javarevisited Newsletter

By Javarevisited

Collection of best Java articles, tutorials, courses, books, and resources from Javarevisite and its authors, Java Experts and many more.  Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Nil Madhab

Written by

Developer @Booking.com | ex: Samsung, OYO | IIT Kharagpur | Entrepreneur, founder of simplecoding.dev | connect me https://twitter.com/Nilmadhabmondal

Javarevisited

A humble place to learn Java and Programming better.

Nil Madhab

Written by

Developer @Booking.com | ex: Samsung, OYO | IIT Kharagpur | Entrepreneur, founder of simplecoding.dev | connect me https://twitter.com/Nilmadhabmondal

Javarevisited

A humble place to learn Java and Programming better.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store