Setting up your first Spring Boot app

Afrar Malakooth
3 min readJul 16, 2020

--

Photo by Sarvenaz Sorour on Unsplash

If you have been following up me through my last couple of Medium stories I have been talking about mobile application development, specifically diving into iOS with Swift. But when it comes to a mobile app it needs to pull and push data from somewhere, it’s when RESTful Web Services come into play. Since Spring Boot being one of the popular choices for building micro-services, I’ll be covering how to set it up on this story. For this tutorial I’ll be assuming that you have a basic understanding of terms like Class and Package with reference to a Java project.

Spring Boot itself if not a framework, it’s a bootstrapped version of the popular Java EE framework called Spring which requires to setup configurations before getting started. What Spring Boot does is preconfigure your Spring application based on the opinion from the community or on how things are generally configured, which can be changed as required.

Let’s navigate to https://start.spring.io on a web browser and get the configuration done with the help of Spring Initializr. Provide values for the placeholders are shown in the screenshot. I’ll be making it as minimum as possible for this demo, but when it comes to actual projects you might need to add multiple dependencies as required.

Spring Initializr

In the above screenshot certain choices have been made based on my personal preference, like I prefer Gradle over Maven. You’re free to choose the build tool, Java version etc. as you prefer. Once you’re done with this click on the generate button and configured app will be downloaded as a zip file. Extract the file into your preferred location and open it on IDE of your choice.

If you navigate to package com.example.demo there should already be a class named DemoApplication.java which the Spring Initializr created for us, which will act as the entry point to our application. Create a new class in the same package and name it as DefaultController.java which we’re going to use as the default REST controller for our application. Inside the newly created class add the below lines of code,

package com.example.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/v1")
public class DefaultController {

@GetMapping("/index")
public String index() {
return "Hello World!";
}

}

Once you’re done with that we have a minimal working Spring Boot app to test. Open a terminal or command prompt window either inside your IDE or from your OS. Make sure that you have navigated inside the project folder from the CLI window. Enter ./gradlew clean assemble and press enter. Gradle will download all the dependencies required for our project.

Once the dependencies are successfully downloaded type ./gradlew bootRun and press enter. Gradle will start the embedded Apache Tomcat server bundled along with Spring Boot and deploy our application inside it. Once the application has been successfully started navigate to http://localhost:8080/api/v1/index on your browser and it will return a Hello Word string as shown in the below screenshot.

Hello World from Spring Boot

In next couple of stories I’ll be demonstrating how to model entities with Spring Data JPA, configure multiple datasources and schedule jobs with Quartz, integration with JIRA etc. I’ll be publishing a DEV Community video based on this story. If you’re interested follow me and keep in touch.

Above is a recent DEV Community video I have published. Also you might be interested to check my Medium story How to add an icon for an iOS app built using Swift or React Native.

--

--

Afrar Malakooth
Afrar Malakooth

Written by Afrar Malakooth

An energetic Software Engineer with 5+ years experience of writing code for leading enterprises. Passionate about social work, volunteering and travelling.