DEV Community

Cover image for Let’s Build Backend for Posts in Our Social Network App using Spring
Nil Madhab
Nil Madhab

Posted on • Originally published at

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

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


We use a lot of social media platforms every day, like Facebook, Linkedin, Twitter, Whatsapp, Instagram. I always wanted to build one myself. So in this series of tutorials, we are going to build one from scratch. It has currently following features now

  1. Users can log in by email or Github.

  2. Users can add each other as friends.

  3. Users can post and see each other’s posts (only text, images and videos are not yet supported)

  4. Users can chat with their friends in whatsapp like interface.

We have used Vue.js as a frontend framework, which we deployed using Netlify and Spring Boot as backend, deployed in Heroku.

In this tutorial, we will discuss the backend of Posts.

Complete Series

Previously we have built, login system and friends features. We will extend to posts feature now.
Let’s Build a Robust Social Login Using Spring Boot, Vue.js and Firebase
Let’s Build Backend for Friends in Our Social Network App using Spring

Live Demo

Pre-Requisites :

  1. The basic knowledge of Java programming language.

  2. An idea of SpringBoot MVC file structure and working principles.

  3. Basic knowledge of creating APIs and working with JSON requests & responses.


  1. Users should able to Post content (API to add a post).

  2. The user should be able to see his own Posts (API to see posts by the user himself).

  3. Users should be able to see all the Posts (API to get all posts).

  4. Users should be able to see the posts of specific users(e.g. friend’s post).

Road Map

  1. Create a Model to store the records in the database in the specified format.

  2. Create a Repository to add custom User-defined methods to interact with the model and get required data.

  3. Create a Service to perform a CRUD operation on the database by using repository methods.

  4. Create a Controller to add API endpoints to interact with Users or Frontend Developers.

Let’s Get Started.


The model defines the structure/format in which the data has to be stored in the database. It represents the java object carrying the data.

create Model class and paste the following code :

Note, that we have declared 3 entities in our Post model Id, User, Content.
  1. The id represents the unique identification of the record.

  2. Content will store the string content provided by the User.

  3. Record creation date and time will be stored into ‘CreatedDate’ column of the Table.

  4. The user_id column will store the id of the user.

Now, we have created a model for Post, let’s create a Repository for the same.


The repository contains the various methods for getting the records from the database. It is also used to create a user-defined method to get records/data from the database.

create file and paste the following code :

Here, we need one custom(user-defined) method to get the user-specific posts. All other methods (e.g. findPostByID,findAllPost) will be provided by default as we extend the JpaRepository interface.

After that, we need to show the latest post at first. So, we have added custom method findAllByOrderByIdDesc . This method will send the posts into reverse order of Id.

Now, Let’s create a service for performing CRUD operations.


The service is used to perform various operations(e.g. CRUD) on the database. The service file is used to write business logic in the code. Nowadays getting data is not enough we need to apply the preprocessing on data. The methods of service class will be useful to extract the data from the database holding specific conditions and rules.

create a file and paste the following code :

Here, we have created 3 methods to save a Post, get the post of a specific user, and get all the posts.
  1. save post method will take a currently logged-in user’s information and the content of the post and save it in the database.

  2. get Post of a user method will take a user id of some user whose Posts the current user wants to see.

  3. get all post method will simply provide the list of all Posts in Reversed order.

Finally, we have created services. Let’s move towards the final step.


A controller is a very important part of Spring Boot MVC. It is used to create API endpoints and interact with users via request and response. It uses the Service that we have created previously to perform the various operation to manipulate the data so that we get all data in the proper format.

paste the following code on the controller file :

Here, we have created 4 endpoints addpost, mypost, posts, /{user_id}/posts .
  1. Add Post is the POST method that will accept the content of the user if a user is logged in and verified and store the content in the database.

  2. My Post endpoint will show the post of the currently logged-in user.

  3. Posts endpoints will not be requiring any authentication as all people can see the posts of other users. It will send all the Posts in response.

  4. The last endpoint /{user_id}/posts is used to get the Posts of the specified users. It will take the user id from the request and pass it to the postService and send posts of the specified user.

Finally, we have completed the implementation of our backend. Let’s test our API endpoints.

Add Post

My Post

Get All Post

Get Post of Specific User

Hurray! It's working.


You have learned to create a Post feature in the Backend. Stay tuned for more upcoming features.

Top comments (0)