Tutorial: Create your first Spring application
Required plugins: Spring and Spring Boot (bundled)
This tutorial describes how to create and run a Spring application in IntelliJ IDEA. It will be a Spring Boot Maven project generated by Spring Initializr. This is the quickest way to create a Spring application, and IntelliJ IDEA provides a dedicated project wizard for it. You will learn how to expose an HTTP endpoint and map it to a method that returns a greeting to the user when accessed through a web browser.
In the main menu, go to File | New | Project.
In the left pane of the New Project wizard, select Spring Boot.
Specify a name for the project:
spring-boot-tutorial
.From the JDK list, select Download JDK and download the latest version of Oracle OpenJDK.
Select the latest Java version.
Click Next to continue.
Select the Spring Web dependency under Web. This dependency is required for any web application that uses Spring MVC.
Click Create to generate and set up the project.
note
Besides the New Project wizard, you can manage Spring Starters later, in existing Spring Boot projects: with your pom.xml or build.gradle file open in the editor, click the Add Starters inlay hint next to the
dependencies
list or press and select Add Starters. This lets you quickly find, add, and remove Spring dependencies in existing projects without having to manually modify the file or thinking about compatible versions.You can disable and enable this inlay hint in the IDE settings (), in Editor | Inlay Hints | Other, under Groovy, Kotlin, and XML.
Spring Initializr creates a class with the main()
method to bootstrap your Spring application. In this tutorial, we'll add the sayHello()
method directly to this class.
Open the SpringBootTutorialApplication.java file under src
/main ./java /com /example /springboottutorial IntelliJ IDEA provides the Go to File action to quickly find and open files. Go to Navigate | File or press , start typing the name of the file and select it from the list.
Add the
sayHello()
method with all of the necessary annotations and imports so that the file looks like this:package com.example.springboottutorial; 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 SpringBootTutorialApplication { public static void main(String[] args) { SpringApplication.run(SpringBootTutorialApplication.class, args); } @GetMapping("/hello") public String sayHello(@RequestParam(value = "myName", defaultValue = "World") String name) { return String.format("Hello %s!", name); } }
The
sayHello()
method takes thename
parameter and returns the wordHello
combined with the parameter value. Everything else is handled by adding Spring annotations:The
@RestController
annotation marks theSpringBootTutorialApplication
class as a request handler (a REST controller).The
@GetMapping("/hello")
annotation maps thesayHello()
method to GET requests for/hello
.The
@RequestParam
annotation maps thename
method parameter to themyName
web request parameter. If you don't provide themyName
parameter in your web request, it will default toWorld
.
IntelliJ IDEA creates a Spring Boot run configuration that you can use to run your new Spring application.
If the run configuration is selected, press .
You can also use the icon in the gutter of the SpringBootTutorialApplication.java file next to the class declaration or the
main()
method declaration.
By default, IntelliJ IDEA shows your running Spring Boot application in the Run tool window.
The Console tab shows the output of Spring log messages. By default, the built-in Apache Tomcat server is listening on port 8080. Open your web browser and go to http://localhost:8080/hello. If you did everything right, you should see your application respond with Hello World!
.
tip
You can quickly open this URL in your browser using the dedicated intention action: place the caret at the controller URL mapping (such as
/hello
), press , and select Open in web browser.
This is the default generic response. You can provide a parameter in your web request to let the application know how to greet you properly. For example, try http://localhost:8080/hello?myName=Human.
The created Spring Boot application has one endpoint available at /hello. However, if you open the root context of your application at http://localhost:8080/, you will get an error because there is no root resource defined. Let's add a static HTML home page with links to your endpoint.
Create the index.html file under
/src ./main /resources /static/ In the Project tool window, right-click the
/src directory, select New | HTML File, specify the name index.html, and press Enter./main /resources /static/ Modify the default template or replace it with the following HTML code:
<!DOCTYPE HTML> <html> <head> <title>Your first Spring application</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p><a href="/hello">Greet the world!</a></p> <form action="/hello" method="GET" id="nameForm"> <div> <label for="nameField">How should the app call you?</label> <input name="myName" id="nameField"> <button>Greet me!</button> </div> </form> </body> </html>
In the Run tool window, click or press to restart your Spring application.
Now your application will serve index.html as the root resource at http://localhost:8080/.
This simple application demonstrates how to get started with Spring. To learn how IntelliJ IDEA helps you write your code and manage the application at runtime, refer to the next tutorial, which focuses on more advanced Spring support features.