Kickstart Spring Boot: Build and Test Your First Web App in Minutes
This tutorial walks you through creating a Spring Boot project with Spring Initializr, explains its lightweight advantages, shows how to set up the project structure, configure Maven dependencies, implement a simple HTTP endpoint, and write unit tests, all using Java and Maven.
Introduction
Spring Boot simplifies Spring development by removing complex configuration, allowing you to create a runnable JAR with a single java -jar command. It speeds up onboarding for all Spring developers, provides sensible defaults, embeds a web container, and eliminates the need for XML configuration.
Quick Start
This article demonstrates how to quickly create a Spring Boot application and implement a simple HTTP request handler, giving you a hands‑on feel for its lightweight and fast development experience.
Create Base Project
Use the official Spring Initializr to generate a Maven project. Select Maven as the build tool, Java as the language, the latest Spring Boot version (e.g., 2.1.3), and add the Web dependency.
After clicking “Generate Project”, unzip the archive and import it into IntelliJ IDEA as a Maven project. Choose the appropriate JDK (Java 8 in this example) during import.
Project Structure
The generated project contains three main parts: src/main/java – the application entry class (e.g., Chapter11Application). src/main/resources – configuration files such as application.properties. src/test – test entry class (e.g., Chapter11ApplicationTests).
Both the application class and the test class can be run directly; without additional web or data modules the program will start and exit after loading Spring.
Project Dependencies
<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/>
</parent>
<groupId>com.didispace</groupId>
<artifactId>chapter1-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<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>The spring-boot-starter-web provides Spring MVC for handling HTTP requests, while spring-boot-starter-test supplies testing utilities.
Write an HTTP Endpoint
Create a package com.didispace.web and add the following controller:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index() {
return "Hello World";
}
}Run the application and send a request to http://localhost:8080/hello (e.g., with Postman). The response will be Hello World.
Write a Unit Test
In src/test/ edit Chapter11ApplicationTests as follows:
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")));
}
}The test uses MockMvc to simulate an HTTP request and verifies that the controller returns the expected string.
Code Example Repository
The complete example can be found in the chapter1-1 directory of the GitHub repository:
GitHub: https://github.com/dyc87112/SpringBoot-Learning/tree/2.x
Gitee: https://gitee.com/didispace/SpringBoot-Learning/tree/2.x
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
