Spring Boot is an awesome module from Spring Framework. Once you are used to it, then working with Spring is a breeze because it takes care of all the spring container specific configurations and allows us to focus more on our application.

REST web services are very popular these days, so in this tutorial, we will see how easily we can create a RESTful web service using Spring Boot.

Spring Boot REST Dependencies

Since web services run on the server, apart from Spring Boot we need to add Spring MVC to our project. I am using Eclipse to create my basic spring boot project, but you can also use Spring Initializr.

When we use Eclipse for creating Spring project, it actually uses Spring Initializr and configures it. Let’s get started.

Spring Boot REST Project

  1. Create new Spring Starter Project as shown in below image. Eclipse should have Spring support for this, all the latest Eclipse release has built-in Spring support.
  2. new-spring-starter-project (1)
  3. In the next popup screen, provide project basic details. I am using Maven, you can use Gradle too.
  4. spring-boot-maven-project-configurations
  5. Next screen asks to add dependencies apart from Spring Boot. Add “Web” dependencies and click next.
    spring boot rest maven dependencies
  6. We could have click on Finish in the earlier screen, but clicking on next provides us the Spring Initializr URL that we can use to create this project easily through the command line.
    spring initializr project urlHere is the complete URL, you can just go to this URL in the browser and download the skeleton maven project that we have created so far.
  7. Below image shows the contents of the auto-generated project
  8. spring-boot-maven-project-configurations.Most important of these is SpringBootRestApplication class that is configured as Spring Boot application and contains java main method. When we run this class, it automatically runs our project as Spring Boot Web project.

    If you will check the console logs, it should print Tomcat started on port(s): 8080 (http) with context path ''.

Now that our base Spring Boot REST application is ready, it’s time to add some endpoints and business logic to it.

We will use a java bean class for sending REST web service response. Here is the code for it.

Spring REST Endpoints Configuration

We will create a @RestController and add some methods and annotate them with @RequestMapping. Below is our REST Controller class that exposes few GET and POST endpoints.

Please go through the above class properly, this is the most important part of this tutorial. It’s very easy to understand what is happening here:

  • When we annotate the controller class with @RestController, Spring Boot scans it to find the REST endpoints to configure.
  • @RequestMapping annotation on methods tells Spring to use this method as handler for any client requests. This is very important spring annotation, we can configure additional features such as HTTP methods supported, Content-Type of request, response content type etc. By default, GET method is supported and JSON response is returned.

That’s it, our Spring Boot RESTful web service is ready to run and test.

Spring Boot REST Web Service Test

When you will run the main class, check the logs for endpoints configuration.

All seems good, GET requests can be tested in the browser itself.

Spring Boot REST GET API

spring boot rest get

spring boot rest get parameter

I am using Postman Chrome app for testing POST requests.

Spring Boot RESTful POST Header Content Type

Spring Boot REST Post Body Response 200 JSON

Spring Boot REST JSON Request Response

JSON is the standard messaging format for REST web services, I didn’t liked that we are accepting String as POST request, let’s change our method to accept JSON request.

That’s it, couple of changes in method annotations and we are done. Now when you will run the main program, it will produce following output for endpoint configuration.

Below image shows the Postman settings and input for the POST method call.



In this post, we learned how to use Spring Boot to create a RESTful web service. Spring Boot did all the configurations and we only had to focus on our business logic. You can download the final project from below link.

By admin

Leave a Reply

%d bloggers like this: