Building REST APIs with Spring Boot

Robel Shanbel

Last Update منذ ٣ أعوام

In this spring rest tutorial, learn to create REST APIs using Spring boot 2 framework which return JSON responses to client. In this Spring Boot 2 REST API tutorial, we will create two simple GET and POST APIs step by step and test them.

1. Maven dependencies

At first, create a simple maven web project and update following spring boot dependencies in pom.xml file.


The important dependencies are spring-boot-starter-parent (read more) and spring-boot-starter-web (read more). Starter web dependency transitively includes more dependencies to build a web application such as spring-webmvc, spring-web, hibernate-validator, tomcat-embed-core, tomcat-embed-el, tomcat-embed-websocket, jackson-databind, jackson-datatype-jdk8, jackson-datatype-jsr310 and jackson-module-parameter-names.

2. Spring Boot 2 REST API Controller

  • In Spring, a controller class, which is capable of serving REST API requests, is called rest controller. It should be annotated with @RestController annotation.
  • The resource uris are specified in @RequestMapping annotations. It can be applied at class level and method level both. Complete URI for an API is resolved after adding class level path and method level path.
  • We should always write produces and consumes attributes to specify the mediatype attributes for the API. Never reply on assumptions.

In given controller, we have two API methods. Feel free to add more methods as needed.


  1. HTTP GET /employees – Returns list of the employees.
  2. HTTP POST /employees – Add an employee in the employees collection.

We can control and customize a lots of implementation details using application.properties file. But to keep this demo simple, I am leaving it blank.

3. @SpringBootApplication

Our REST APIs skeleton is ready. Now we need to configure Spring to detect our rest controller (using auto scanning) and deploy apis in embedded tomcat server. Thankfully, Spring boot makes all these things very easy by using the concept of auto configuration.


Auto-configuration attempts to guess and configure beans we you are likely to need. Auto-configuration classes are usually applied based on the jars in application classpath and the beans we have defined additionally in @Configuration classes.

In this case, it does following things.


  1. It detects spring-webmvc so configure default spring mvc application beans. It help in scan and configure @RestController and similar annotations.
  2. It detects embed tomcat jars so configure embedded tomcat for us.
  3. It detects JSON jars so configure JSON support to APIs.

4. Model classes and DAO

These classes are not directly related to REST. Still lets take a look how they have been written.

DAO class uses a static list to store data. Here we need to implement actual database interaction.

5. Spring Boot REST Demo

To start the application, run the main() method in SpringBootDemoApplication class. It will start the embedded tomcat server. In server logs, you will see that API have been registered in spring context.

5.1. HTTP GET /employees

Once server is UP, access the API using some rest client.

Spring Boot REST HTTP GET

5.2. HTTP POST /employees

Spring Boot REST HTTP POST

Was this article helpful?

1 out of 1 liked this article