How To Create a Basic RestAPI Application with Spring Boot

Hi guys! In this article we are going to create a basic RestAPI application with Spring Boot! At the end of this article we can get the list of cars with GET method.

Photo by Rich Tervet on Unsplash

First of all we need to create a basic Spring Boot project. You can do that with Spring Initializr. If you don’t know how to do that, don’t worry! You can find the details here. Please read the article and come back! But if you know that just keep reading.

After creation of the project. We have this roadmap.

  • Create a RestAPI Controller
  • Create a Car class
  • Create a GET method
  • And test the method

Let’s start! First we need to create our controller. Let’s name it “”

This class will be our Rest Controller so we are going to use the @RestControllerannotation. This annotation will do a lot of job for us.

After that I want to reach this controller from the address localhost:8080/api. That will be better for users. So I am adding new one additional annotation for that purpose. It is @RequestMapping(“/api”)”. After that we will be able to reach our API via “localhost:8080/api”.

Now let’s create a Car class. You can create any class that you want to send via your API. I choose Car here. First create new package under the main package. I am creating a new package named “entity”. And our Car class will be,

So let’s define some class variables. I am defining three variables here, they are “company”, “model”, “color”. After that please create getter and setter methods, and also constructor method with all variables so that we can easily create new cars for our example. So finally our car class will be,

Now it’s time to create our GET method to get the list of cars via our RestAPI controller. Let’s create a method under our controller class.

This “getCars” method will return the list of created cars, and we can use this method to GET those list via our API. So our magical annotation will help us a lot at this point. Let’s add “@GetMapping(“/cars”)” annotation to our method. This will be our GET method when we cant to get the list of cars. So that we can reach the list of cars via “../api/cars”.

So let’s write some code to create cars, put them to the list and return them for our api. I have created six cars for our example and putted them to the list, and finally return them.

Our Controller class after all,

After all, let’s save our project and run it.

Let’s test our application. You can test it on postman or just in your browser. Let’s see both of them to cover everything. Our browser shows us,


And that’s it! We have successfully created our first RestAPI Application. Yes it is so basic but we can move on from that point. We can add some methods to even add a car to the list or delete one. Please take some time and practice this example. You can find the Github repository for the source code below.

That’s all for now. I hope that article will help you, if you have any questions or suggestions please leave me a comment. Thank you for your time. Hope to see you in next articles!

Full-time developer, and lifelong learner. Loves to learn and experience. Motto: Stay Hungry, Stay Foolish | @anilemrah_

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