Introduction
When you first encountered and learned the Spring framework, were you discouraged by its complex configuration? After using Spring for the n-th time, are you tired of repeatedly copying and pasting the same configuration files? If so, it’s time to try Spring Boot—a powerful tool that makes getting started easier and building Spring applications faster and simpler.
Spring Boot makes your Spring applications more lightweight. You no longer need to go through the tedious process of setting up projects, packaging applications, and deploying them to application servers like Tomcat. With Spring Boot, you can package your service into a single JAR file containing just one Java class, and run it directly with the java -jar
command. Compared to traditional Spring applications, this approach is significantly more convenient and streamlined.
Here are the main advantages of Spring Boot:
- Enables faster onboarding for all Spring developers
- Out-of-the-box: Provides various default configurations to simplify project setup
- Embedded containers simplify web project development
- Eliminates the need for redundant code generation and XML configuration
Getting Started Quickly
In this tutorial, we will learn how to quickly create a Spring Boot application and implement a simple HTTP request handler. Through this example, you’ll gain a preliminary understanding of Spring Boot and experience its simple structure and rapid development capabilities.
Creating a Basic Project
Spring provides a convenient tool called Spring Initializr to help us create Spring Boot projects.
Using the Spring Initializr Web Page
Step 1: Visit https://start.spring.io
As shown in the image, here are the key options:
- Project: The build tool to use—Maven or Gradle. This tutorial uses Maven, which is more familiar to most Java developers and makes the content more accessible.
- Language: The programming language—Java, Kotlin, or Groovy. We’ll use Java for consistency and broad compatibility.
- Spring Boot: The Spring Boot version. We’ll use the latest version available at the time: 2.1.3.
- Project Metadata: The project’s metadata—essentially the basic elements of a Maven project. Clicking "More options" reveals additional settings. Fill in the fields (like Group, Artifact, etc.) according to your organization’s needs.
- Dependencies: The Spring Boot components to include. Since we’ll be implementing an HTTP interface, select the Web module. Simply type "Web", and the page will auto-suggest matching components.
After clicking the "+" button, you should see something like this:
(Image reference: Web dependency added)
Step 2: Click the "Generate Project" button. Your browser will download a compressed file named after the Artifact.
Step 3: Extract the project archive and import it into your IDE as a Maven project. For example, in IntelliJ IDEA:
- Go to File → New → Project from Existing Sources...
- Select the extracted project folder and click OK.
- Choose Import project from external model and select Maven, then click Next until the import is complete.
- If you have multiple JDK versions, ensure you select Java 8 during setup (or the version you specified in Step 1).
Since we’ll be creating many sample projects, consider organizing them as modules within a single parent repository, similar to the example repository mentioned at the end of this article.
Creating the Project in IntelliJ IDEA
If you're using IntelliJ IDEA for Java development, you can create a Spring Boot project directly within the IDE.
Step 1: From the menu, select File → New → Project.... You’ll see a window like the one below:
The Initial Service URL points to the official Spring Initializr service, meaning this IDE feature is based on the same web tool.
Step 2: Click Next. After a brief moment, you’ll see the project metadata window:
The content is identical to the web-based Spring Initializr. Fill in the fields just as you would on the website.
Step 3: Click Next again to reach the window for selecting the Spring Boot version and dependencies:
Note that this interface includes not only Spring Boot Starter POMs but also various Spring Cloud dependencies.
Step 4: Click Next once more to configure the project’s local storage details. Finally, click Finish to complete the project creation.
Although IntelliJ’s Spring Initializr is still based on the official web tool, it calls the service directly from the IDE and automatically builds the project in your local file system. This streamlines the entire process. If you haven’t tried it yet, Spring Boot and Spring Cloud enthusiasts should definitely give it a try!
Project Structure Overview
After completing the steps above, your basic project is created. As shown in the image, a Spring Boot project typically contains three core files (exact paths may vary based on the Group ID you provided):
-
src/main/java
: The application entry point, e.g.,Chapter11Application.java
-
src/main/resources
: Configuration files, such asapplication.properties
-
src/test/
: The test entry point, e.g.,Chapter11ApplicationTests.java
Both Chapter11Application
and Chapter11ApplicationTests
can be run directly to start the project. However, since no data access or web modules are configured yet, the application will load Spring and then terminate.
Project Dependencies Explained
Open the pom.xml
file to examine the project dependencies:
<?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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.didispace</groupId>
<artifactId>chapter1-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>chapter1-1</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>
Project Dependencies Explained
The pom.xml
consists of four main parts:
- Project Metadata: The basic Maven project elements (groupId, artifactId, version, name, description, etc.) filled in during project creation.
- Parent: Inherits from spring-boot-starter-parent, which manages dependency versions and build configurations.
-
Dependencies: The actual project dependencies:
-
spring-boot-starter-web
: Enables HTTP endpoint support (includes Spring MVC). -
spring-boot-starter-test
: Provides libraries for writing unit tests. We'll explore more starter modules in later tutorials.
-
- Build Configuration: Uses the spring-boot-maven-plugin to package the Spring Boot application into an executable JAR file, thanks to the parent POM.
Creating an HTTP Endpoint
- Create a package named
com.didispace.web
(adjust according to your group ID). - Create a class
HelloController
with the following content:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index() {
return "Hello World";
}
}
3.Run the main application class (e.g., Chapter11Application.java).
4.Use a tool like Postman or your browser to send a GET request to:
http://localhost:8080/hello
✅ You should see the response:Hello World
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Chapter11ApplicationTests {
private MockMvc mvc;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
}
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World")));
}
}
We use MockServletContext to create an empty WebApplicationContext, allowing the HelloController to be instantiated in the @Before method and passed to MockMvcBuilders.standaloneSetup().
Note: Make sure to include the following static imports to enable status, content, and equalTo:
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Conclusion
You've now completed the tutorial!
Top comments (0)