Building a Spring Boot application using IntelliJ IDEA

In this quick post, we will learn about building a Spring Boot application using IntelliJ. The main purpose of this post is to help anyone new to Spring Boot get rolling quickly writing Spring applications with Spring Boot in IntelliJ. If you are a beginner with Spring Boot, I highly recommend starting with our Spring Boot.

 

Introduction

IntelliJ IDEA is the most popular Java IDE.IntelliJ IDEA  provides first-class support to create and run Spring Boot based applications. We can create and configure Spring Boot application in the following ways.

  • Import Spring Boot project in IntelliJ IDEA.
  • Use built-in support for Spring Initializr in the IntelliJ IDEA  editor.

We are using the built-in support of the IDE to create our Spring Boot application.

Spring Boot is available in the Ultimate edition only.

 

1. Create Spring Boot Project With IntelliJ

To start, the process, open up IntelliJ IDEA and click on the new project option.

Build a Spring Boot application using IntelliJ IDEA

 

2. Select Spring Initializr from the project type on the left side panel. Select the correct JDK from the Project SDK drop-down (Spring Boot 2.x require minimum JDK 8).

Build a Spring Boot application using IntelliJ IDEA

 

3. Enter the Maven project properties as per your project requirements and click on the next step.

Build a Spring Boot application using IntelliJ IDEA

 

4. Select the Spring Boot version and other required dependencies for your project. Based on the selected dependencies, it will add correct Spring Boot Starters in the pom.xml file.

Build a Spring Boot application using IntelliJ IDEA

 

5. In the last part of the wizard, we need to select a project name and project location. Once selected click on the “Finish” button.

Build a Spring Boot application using IntelliJ IDEA

 

Once finished, IntelliJ IDEA import all the required dependencies and open the newly created project to work on.

Build a Spring Boot application using IntelliJ IDEA

 

2. The pom.xml file

The pom.xml file contains all the building blocks for the Spring Boot application. Open the pom.xml file located in the root of the project module.

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.javadevjournal</groupId>
   <artifactId>demoproject</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>sample-project-IntelliJ</name>
   <description>How to create Spring Boot project using IntelliJ</description>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.1.RELEASE</version>
      <relativePath />
      <!-- lookup parent from repository -->
   </parent>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </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>

We added spring-boot-starter-parent as the parent of our Spring Boot project. This parent provides a number of features in the Spring Boot application

  • Configuration – Java Version and Other Properties.
  • Dependency Management – Version of dependencies
  • Default Plugin Configuration

For more detail, read What is Spring Boot

 

3. Spring Boot Main Application Class

When we created this project, Spring Initializr created the following class automatically to start our Spring Boot application.

package com.javadevjournal.demoproject;

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

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

 

4. Run Application

Spring Boot main class contains the main method. This is just a standard method that follows the Java convention for an application entry point. Our main method delegates to Spring Boot’s SpringApplication class by calling runSpringApplication bootstraps our application, starting Spring. We can run our application from the IntelliJ by clicking on the run application icon

Build a Spring Boot application using IntelliJ IDEA

As an alternative, we can use the run goal to start our application. Type mvn spring-boot:run from the root project directory to start the application. You should see output similar to the following.

Build a Spring Boot application using IntelliJ IDEA

 

Read our article Building an Application with Spring Boot to build a web application using Spring Boot

 

Summary

In this small post, we covered the process of building a Spring Boot application using IntelliJ. IntelliJ is really a powerful editor and provides first-class support to create and run Spring Boot based web applications.

Umesh

Hello!! I am Umesh- an engineer by profession and a photographer by passion.I like to build stuff on the web using OSS and love to capture the world through my lens.

follow me on:

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
littleOStar Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
littleOStar
Guest
littleOStar

good job