Build a REST API with Spring Boot — Part 1 : Introduction & Environment setup

Yassine El Baaj
3 min readNov 16, 2019

--

In this tutorial, I will be introducing you to how you can build a REST API very quickly using Spring Boot Framework. This first part is meant to wet your appetite and let you get a basic understanding of REST APIs and Spring Boot as well as setting up your environment.

What is an API ?

API is the acronym of Application Programming Interface, which is basically a piece of software that allows two different applications to communicate with each other without knowing the details of their implementation.

What is a REST API ?

REST stands for Representational State Transfer. It represents an architectural style for APIs. Using a REST API on the internet means interrogating a third-party server using the HTTP protocol. Thus we will interrogate the server through a predefined URL or endpoint ( which is commonly provided by the API editor ). These interrogations will be done according to different HTTP methods such as : GET, POST, PUT, DELETE etc…
As a general rule, when sent, a request returns a response. When you are communicating with a REST API, you will usually get a response in JSON or XML format.

REST API Example

Instagram : The Instagram REST API allows your application to access user accounts, photos, tags and more. Here is a GET request sent to the Instagram API to get the photos of a peculiar place using its longitude and latitude. In this example, it is in New York (40.7127° N, 74.0059° W) :


GET /v1/locations/search?access_token=ACCESS_TOKEN&lat=40.7127&lng=74.0059

And this is the response in JSON format :

HTTP/1.1 200 OK{
“meta”: {
“code”: 200
},
“data”: [
{
“latitude”: 40.714198749,
“id”: “93496093”,
“longitude”: 74.006001183,
“name”: “John’s Pizzeria 278 Bleecker St NY, NY”
},
{
“latitude”: 40.7142,
“id”: “46371155”,
“longitude”: 74.0064,
“name”: “Thunderpocalypse 2012”
},
...
]
}

You can see that we are getting a lot of information regarding the places near the location mentioned within the URL.

What is Spring Boot ?

Spring Boot is Framework for building Java applications very fast. It is so solid, that there is no need for you to reinvent the wheel. It provides a lot of features such as : Database connectivity, Template engines, Authentication, Messaging and a lot more.

Spring Boot Advantages

First of all, it makes use of Java, which is one of the most used programming languages in the world. Besides that, it has a very large community and a large amount of free learning materials .
Some additional benefits include:

  • Reduces development time and increases the overall productivity of the development team.
  • Makes it easier for developers to create and test Java-based applications by providing a default setup for unit and integration tests.
  • Avoids writing lots of boilerplate code, annotations, and XML configuration.
  • Comes with embedded HTTP servers like Tomcat or Jetty to test web applications.

What We Will Be Building

We will build a REST API to manage Books. It will allow us to fetch all books, create a book, update a book and delete an existing book.

Tools

  • Java Development Kit 1.8 or a superior version.
  • IntelliJ IDEA Community Edition ( You can use another IDE like Eclipse or NetBeans ).

Creating Your First Project

For the sake of simplicity, we will use the service Spring Initializr. Spring Initializr provides an extensible API to generate JVM-based projects.
Get to the page and follow these steps :

  • Choose Maven Project.
  • Choose the version 2.1.10+ of Spring Boot.
  • Enter the name of your project in the Artifact field.
  • In the dependencies section, select the following : Web, JPA, H2 and Lombok.
  • Click on Generate.
  • A .zip will download. Unzip it. Inside it you will find a simple, Maven-based project containing a pom.xml file.
  • Open IntelliJ IDEA and choose Open.
  • A window will be displayed, select the pom.xml file of your project in the filesystem and choose Open as project.

That’s it ! We have successfully set up our environment and we are ready to implement the main features of our REST API !

Read the next Tutorial here.

--

--