How to build an application with Spring Boot

Tech Insights

Spring Boot is a framework designed to simplify the development of Java applications, particularly web applications. It helps you to create Spring-powered, production-grade applications and services with absolute minimum fuss. Since the launch of Spring Boot, way of building stand-alone, production-ready applications has been more or less streamlined. This guide is meant to help you understand how to build an application with Spring Boot.

Building an Application with Spring Boot

Please note that this guide assumes you have:

Spring Boot allows you to create stand-alone Java applications that can be started using java -jar or more traditional WAR deployments. Following are the steps to build a simple application in Spring Boot:

1) Create a new Spring Boot project:

You can either create a Spring Boot Project from scratch with the help of Spring initializr or by downloading the source repository from Git.

1.1) Using the Spring initializr

Spring Initializr is a web-based tool that helps you quickly generate an initial structure of a Spring Boot project. It includes the necessary dependencies and configurations based on your specifications to bootstrap your project.

  • Open the Spring initializr – https://start.spring.io
  • Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java. Now provide the Group and Artifact name. You can see the Dependencies section on the right side in which you select the required dependencies for your project.
  • Select the packaging type(jar/war) and click on the Generate button to pack the project in a .rar file and download it.

How to build an application with Spring Boot

1.2) Download or Clone the source repository

Download the project from here or clone it using Git:

git clone https://github.com/spring-guides/gs-spring-boot.git

cd into gs-spring-boot/initial

2) Import the project to your IDE

Choose the Import -> Existing Maven Project option in your IDE to import the downloaded project. Following image shows the project directory:

How to build an application with Spring Boot

You can see the application class – DemoApplication created by the Spring Initializr.

package com.infobrisk.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

}

Following is the default pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.1.1</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.infobrisk</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

You will add dependencies as your application grows. Now to run your application, right click on DemoApplication.java class and choose Run As -> Java Application. You should be able to see the logs as shown below:

How to build an application with Spring Boot

You can create a web controller to add methods and mapping URLs to access them at localhost:8080