Getting Started with Spring Boot

Gayani Nisansala
5 min readSep 19, 2021

--

Start your journey with Spring Boot from basics…

In this article I am going to start a article series about Spring Boot which is a Java-based framework used to build stand-alone and production ready applications.

Prerequisites- Knowledge of Java programming language is needed.

In the tutorial, following steps will be discussed.

  • Step 1: Creating the Spring Boot project using Spring Initializr.
  • Step 2: Importing the Spring Boot project to the IDE.
  • Step 3: Implementing an API to retrieve list of books.

Step 01

As the first step, I will go through the process of creating a new Spring Boot application using Spring Initializr.

Visit the Spring Initializr site and fill your options such as Project type, Language, Spring Boot version etc which are needed to create your Spring Boot project. The link to the Spring Initializr is available below.

Filling up the project details

Also you can add the dependencies which are needed to the project. In this example project I will add the “Spring Web” dependency because I am going to build a web, including RESTful application.

Adding the dependencies to the project

After filling the project details and adding dependencies, the project is ready to be created.

Final view before generating the project

Then generate the bundled up project as a zip file and it will be downloaded to the local machine. Then you can move the .zip file to a desired file location and unzip the file.

unzipping the .zip file in a desired file location

Step 02

Now open your favorite IDE which you are planning to continue the developments. I am using the Eclipse IDE for the developments.

Import the unzipped Spring Boot project to the IDE. You can import the project as a existing maven project.(File → Import → Maven → Existing Maven Project)

Importing the project

While importing the project to the IDE, all the dependencies which are stated in the pom.xml file will be downloaded. You can find those downloaded dependencies in the “Maven Dependencies” folder.

Imported Spring Boot project

Important- Sometimes you may get some errors in pom.xml file according to the Spring Boot version. In that kind of situations first apply maven update to the project( Right click on the project and visit Maven → Update Project). If the error still exists, search the error to find the solution.

Step 03

Finally you can check whether the application is running successfully by run the application as a java application as below.

Running the application

If you get the console output as below, then the application has been run successfully.

Console output

Step 04

In this step, let’s implement a API to retrieve all books which are hardcoded. In this step we are not using any database connectivity and just using static list of books for the demonstration.

Let’s first create Book.java class as below.

Creating the Book.java

Then define the member variables of the Book.java class such as id, name, authors, and price. Also add getters and setters for the member variables as well as constructor.

package com.jg.springboot.basics.springbootbasics.book;import java.util.List;public class Book { private long id;
private String name;
private List<String> authors;
private double price;

public Book(long id, String name, List<String> authors, double price) {
super();
this.id = id;
this.name = name;
this.authors = authors;
this.price = price;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getAuthors() {
return authors;
}
public void setAuthors(List<String> authors) {
this.authors = authors;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}

}

Then let’s create the BookService.java class.

Creating the BookService.java

In the BookService class, let’s initialize a ArrayList which is having the type of Book class. Then add a static block to add Books to the list as follows.

package com.jg.springboot.basics.springbootbasics.book;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Service;@Service
public class BookService {
public static List<Book> books = new ArrayList<Book>();
public static long idCounter = 0;
static {
books.add(new Book(++idCounter,
"Cloud Native Java: Designing Resilient Systems with Spring Boot, Spring Cloud, and Cloud Foundry",
Arrays.asList("Josh Long", "Kenny Bastani"), 45.99));
books.add(new Book(++idCounter,
"Spring Boot in Action",
Arrays.asList("Craig Walls"), 25.88));
}

public List<Book> findAll(){
return books;
}
}

Let’s then create BookResouce.java class as below.

Creating the BookResource.java

Then add a method as below to control the GET requests which has the path of “/books”.

package com.jg.springboot.basics.springbootbasics.book;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BookResource {
@Autowired
private BookService bookService;
@GetMapping(path = "/books")
public List<Book> getAllBooks() {
return bookService.findAll();
}
}

Then terminate the current up and running Spring Boot application and re-run the application.

Then open the web browser and browse http://localhost:8080/books. Then you can get the list of books which are stated in the BookService.java.

Result of the GET request

Amazing.. We have just created an API which can retrieve all the books which are retrieved in the book service. In the next article, detailed description of the annotations will be provided. Hope you enjoyed the content of the article.

If you have any doubt please reach out to me. Happy practicing… 👩‍💻

--

--

Gayani Nisansala

Following B.Sc.(Hons) in Information Technology at University of Moratuwa, Sri Lanka