How to create your first spring boot application using IntelliJ IDEA 2021

Rusiru Thushara
4 min readMay 26, 2021

--

Here in this tutorial, you will understand how to create your first web application using spring boot. Here, I am using IntelliJ IDEA as my integrated development environment(IDE).

Why Spring Boot?

Spring Boot is a very powerful framework that provides a flexible way of configuring Java Beans, XML configurations, and Database Transactions. Also, it facilitates powerful batch processing and manages REST endpoints. In Spring Boot, no manual configurations are needed; Everything is auto-configured.

How To Get Started

Now, in order to get started with spring boot let’s go ahead and navigate to spring initializr where we can bootstrap any given spring boot application.

Here you can change the settings as you want but for this tutorial, I am gonna leave everything as default. However, we need to get some dependencies for our application. Click on ADD DEPENDENCIES.

Here, we can pick the dependencies that our application needs. For this to keep things simple I’m gonna use only the following dependency.

Spring Web — Build web, including RESTful, applications using Spring MVC. Uses Apache Tomcat as the default embedded container.

Keep pressing Ctrl while adding multiple dependencies. After selecting all of the required dependencies press Esc.

Finally, click on GENERATE and it will download a .zip file automatically.

Now you can extract it and open the extracted folder with IntelliJ IDEA.

You will get the following folder structure.

If you go to the pom.xml file you will see all the dependencies that we are using. You can easily add or remove dependencies there as you need.

Let’s say Hello! to yourself

Open the DemoApplication.java file under the /src/main/java/com/example/demo.

Add the sayHello method so that the file looks like the following.

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

@GetMapping("/hello")
public String sayHello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}

}

Here, we are using some annotations. Let’s identify each of them.

@SpringBootApplication makes a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning.

@RestController creates RESTful web services using Spring MVC.

@GetMapping maps HTTP GET requests onto specific handler methods.

@RequestParam maps the ‘name’ method parameter to the ‘name’ web request parameter. If you don’t provide a name parameter in your web request, it will default to World.

Let’s add a homepage

Now, create an index.html file under /src/main/resources/static/.

In the Project tool window, right-click the /src/main/resources/static/ directory, select New and then HTML File, specify the name as index and then hit Enter.

You can replace it with the following HTML code.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body>
<form action="/hello" method="GET" id="nameForm">
<div>
<label for="nameField">Enter your name : </label>
<input name="name" id="nameField">
<button>Greet me!</button>
</div>
</form>

<p><a href="/hello">Greet the World!</a></p>

</body>
</html>

You are all set.

Let’s run the application

You can just press Shift + F10 or use the arrow icon in the gutter of the DemoApplication.java file next to the class declaration or the main method.

You will see that the built-in Apache Tomcat Server started on port 8080. Now open your web browser and go to http://localhost:8080/ You will see your Hello World application there.

Congratulations, you created your first spring boot application.

--

--

Rusiru Thushara

Computer Engineering Undergraduate at University of Peradeniya