Spring

Spring Boot: Building a RESTful Web Application

Introduction:

REST stands for Representational State Transfer and is an architectural guideline for API design. We assume that you already have a background in building RESTful APIs.

In this tutorial, we’ll design a simple Spring Boot RESTful web application, exposing a few REST endpoints.

Project Setup:

Let’s start by downloading the project template via Spring Initializr:Spring Initalizr RESTful application

We only need to add ‘Spring Web’ as an extra starter dependency for RESTful web applications. We have added the other two assuming we’re interacting with the database as well.

POM file:

Our POM file will now have all the needed web application and database dependencies:

REST Controller:

Let’s now define our REST controller:

We can define all our GET, POST, DELETE or PUT mappings in our controller.

Service:

Here, the StudentService is the class interacting with the database and doing all the operations for us:

We have another tutorial explaining how to configure the H2 database with Spring Boot.

Running the Application:

Finally, we can run our UniversityApplication class:

With which our REST endpoints will be exposed on the embedded server.

Testing REST Endpoints:

Let’s use cURL to test out our REST endpoint:

This will return all the student records present in our database:

Similarly, we have:

We can also use POSTman tool to test our endpoints. It has a great user-interface.

Conclusion:

In this tutorial, we built a Spring Boot RESTful application from scratch. We exposed a few API and then tested them using cURL.

2 comments

ProgrammerGirl you are too Good,, please add some real time concepts which will help in projects like spring with OAuth 2, jwt, Docker and K8s etc, if possible aws….

Albert Einstein said: if you can’t explain it in simple way then you didn’t understand it well.
ProgrammerGirl you explained in simple way effectively …

Leave a Comment

Your email address will not be published. Required fields are marked *