Kickstart Spring Boot: Build Your First Web Service in Minutes
This guide walks you through creating a basic Spring Boot project with Maven, adding the web starter, implementing a simple Hello World REST controller, and writing unit tests using MockMvc, providing step‑by‑step instructions, code snippets, and project structure details to help Java developers quickly get started with lightweight Spring applications.
Overview
Spring Boot simplifies Spring development by providing auto‑configuration, an embedded servlet container and a convention‑over‑configuration approach. This guide shows how to create a minimal Spring Boot project with Maven, add the web starter, implement a “Hello World” REST endpoint, and verify it with a unit test using MockMvc.
Prerequisites
Java 8 (or Java 7+) installed.
Maven 3.x.
Spring Boot 1.3.2 or later.
Project generation
Use Spring Initializr (http://start.spring.io) to generate a Maven project. Select the desired Group, Artifact, and Spring Boot version, then download and extract the ZIP.
Directory layout
src/main/java– contains the application entry class (e.g., Chapter1Application). src/main/resources – holds application.properties and other resources. src/test/java – contains test classes (e.g., Chapter1ApplicationTests).
Configure Maven dependencies
Add the core starter, test starter and web starter to pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>Implement the Hello World controller
Create a package (e.g., com.didispace.web) and add the following class:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index() {
return "Hello World";
}
}Run the application (the generated Chapter1Application contains the main method). Access http://localhost:8080/hello in a browser; the response body should be Hello World .
Write a unit test with MockMvc
Create a test class under src/test/java:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
public class Chapter1ApplicationTests {
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")));
}
}Import the static matchers used in the test:
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;Build and run
From the project root execute mvn clean package to compile and package the application.
Run the executable JAR with java -jar target/artifactId-0.0.1-SNAPSHOT.jar (replace artifactId with your project name).
The embedded Tomcat starts on port 8080; the endpoint and unit test can be exercised as described above.
Key points
Spring Boot’s auto‑configuration eliminates XML and boilerplate code.
The spring-boot-starter-web starter brings in Spring MVC and an embedded servlet container.
MockMvc enables fast, isolated controller tests without starting the full server.
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.
