Posts Java spring boot + intellij idea
Post
Cancel

Java Spring Boot + IntelliJ IDEA

Java and spring boot logo with a plus sign followed by intellij idea logo

Intro

To those who develop in java nowadays, it’s almost impossible to miss Spring framework and more specifically Spring Boot. Using this development stack, we gain more productivity and agility from small to large sized java projects. In this guide I’ll demonstrate how to install, configure IntelliJ IDEA and create a simple Hello-World using java, IntelliJ IDEA and spring boot.

The github repository of the example project of this post, can be found at: https://github.com/danielpadua/java-spring-idea-example

Requirements

  • Java JDK 8 or higher

Installing IntelliJ IDEA

Use the following sections based in which operational system you’ll be using:

Windows

In Windows we have 2 options:

Open powershell and install IntelliJ IDEA using the following command line:

1
choco install intellijidea-community

Linux

In Linux we also have 2 options:

macOS

In macOS we have 2 options again:

  • Direct download

    Go to jetbrains download page, select the latest version (on the writing date of this guide is the 2019.1.1) and install it as usual, dragging the app from the .dmg file to the apps folder of your mac.

  • Using a package manager (Homebrew)

    If you don’t know Homebrew, take a look at this post

Open your favorite terminal and install IntelliJ IDEA using the following command line:

1
brew cask install intellij-idea-ce

Configuring IntelliJ IDEA

With IntelliJ IDEA installed, the configuration is pretty simple. When you execute IDEA you will be questioned about some configurations like: theme color, shortcut key mapping and plugins. Let’s leave default config and begin.

Creating the project

As soon as it starts, you’ll see the following screen:

IntelliJ IDEA initial screenIntelliJ IDEA initial screen

For this example, we’ll be using Maven as build-tool.

Unfortunately using IntelliJ IDEA Community, according to the documentation, there’s no support to create Spring Boot projects using Spring Initializr through the IDE in Community version, only in the Ultimate Edition. So, we have two choices that we can explore:

Use Spring Initializr Web

Access: https://start.spring.io, and fill the fields like below:

Configuring project creation in Spring InitializrConfiguring project creation in Spring Initializr

Don’t forget to select Web as dependency. Click Generate Project to download the project zip file. Extract it to a directory of your choice, go back to IntelliJ IDEA and select Import Project. Navigate to project’s directory and select the pom.xml file. You’ll see a window that is responsible for importing the Maven project, leave the defaults configs:

Import Spring Initializr projectImport Spring Initializr project

Select the project to import and click next:

Select the projectSelect the project

In the next screen, set the JDK version that you installed:

Select the JDK version for the projectSelect the JDK version for the project

In the next screen confirm the project name and click finish.

Creating a Maven project and add Spring manually

In IntelliJ IDEA’s initial screen, select Create New Project, located on the left side tab and select Maven, on the right side, select the JDK version and click next:

Creating a Maven projectCreating a Maven project

When selecting the archetype, IntelliJ IDEA will assume that you will use Quickstart archetype, which is ok for our goal.

In the next screen specify the GroupId, ArtifactId and the Version and click next:

Maven configurationMaven configuration

After you just have to name your project and click finish:

Naming the projectNaming the project

With the project created, configure pom.xml according to the following snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>br.com.danielpadua</groupId>
	<artifactId>java-spring-idea-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>java-spring-idea-example</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</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-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>

After updating pom.xml a notification will pop-up at the bottom right corner of the screen:

Import pom.xml changesImport pom.xml changes

Click Import Changes for Maven refresh all project dependencies.

Now we’ll create a class that will contain the main function of the project. Remember that creating a class in default package is not a good java practice, so, click in source folder main/java and create a package:

Creating a packageCreating a package

Write the name of the package, in my example was: br.com.danielpadua.java_spring_idea_example, and create a class inside this package named ExampleApplication.java, and write the following code:

1
2
3
4
5
6
7
8
9
10
11
package br.com.danielpadua.java_spring_idea_example;

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

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

Don’t forget the unit tests main class too, repeat package creation step and create a class named: ExampleApplicationTests.java and write the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package br.com.danielpadua.java_spring_idea_example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ExampleApplicationTests {
    @Test
    public void contextLoads() {
    }
}

At this moment we’ll have basically the same project structure that is generated by Spring Initializr at the section above.

Completing the project

With the basic project skeleton created, we just have to create a package to nest the controller that will contain Hello World endpoint. Right click the root package:

Creating a package for the controllersCreating a package for the controllers

Write: controllers and confirm. Inside the generated package, create a class named: ExampleController.java and write the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package br.com.danielpadua.java_spring_idea_example.controllers;

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

/**
 * ExampleController
 *
 * @author danielpadua
 *
 */
@RestController
@RequestMapping("/api/example")
public class ExampleController {

    @GetMapping("/hello-world")
    public ResponseEntity<String> get() {
        return ResponseEntity.ok("Hello World!");
    }

}

Run the project by right clicking over the main class Application.java and select the option Run ‘Application.main()’:

Running the appRunning the app

After clicking run, you should see the output in the Run tab located at screen’s bottom:

Spring initialization logSpring initialization log

To test the app, you only have to open your favorite browser and access: http://localhost:8080/api/example/hello-world, and you should see the Hello World message:

VoiláVoilá

Conclusion

IntelliJ IDEA is the most used IDE for java nowadays and it’s probably the most complete in the opinion of many developers. Community version is a great alternative to traditional eclipse.

See you soon!

This post is licensed under CC BY 4.0 by the author.