Master Spring Boot: From Project Creation to Hello World in Minutes

This guide walks through the fundamentals of Spring Boot, explaining its purpose, showing how to create a project with Spring Initializr, writing a Hello World REST controller, understanding starter dependencies, configuring properties, exploring the main application class annotations, and adding a simple unit test.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Master Spring Boot: From Project Creation to Hello World in Minutes

Introduction

Java developers are likely familiar with older frameworks such as SSM or SSH, but this article focuses on Spring Boot, a modern framework that simplifies Spring application development by providing sensible defaults and reducing configuration overhead.

What Is Spring Boot?

Spring Boot, provided by the Pivotal team, is the entry point for projects based on Spring Framework 5.0. Its design goal is to let you run Spring applications quickly with minimal configuration, following the "convention over configuration" principle.

How to Set Up a Spring Boot Project?

The easiest way for beginners is to use the Spring Initializr integrated in IntelliJ IDEA. The steps are:

Select File → New → Project and choose Spring Initializr.

Specify the JDK version (e.g., 1.8) and click Next.

Enter Maven coordinates, package name, and other metadata, then click Next.

Choose the required starter dependencies (e.g., spring-boot-starter-web) and the Spring Boot version, then click Next.

Set the project name and location, and click Finish to generate the project.

After creation, the project contains a main class DemoApplication with a main() method and an application.properties file for configuration. Running the main method starts an embedded Tomcat server on port 8080.

First Program: Hello World

To demonstrate a simple web endpoint, add the spring-boot-starter-web dependency (if not already present) and create a controller:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
    @RequestMapping("/hello")
    public String helloWorld() {
        return "Hello World";
    }
}

Access http://127.0.0.1:8080/hello to see the response.

Dependency Analysis

The generated pom.xml includes a parent spring-boot-starter-parent that manages versions for all starter dependencies, so you typically do not need to specify versions for individual starters.

For example, spring-boot-starter-web brings in:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>2.3.4.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-json</artifactId>
        <version>2.3.4.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <version>2.3.4.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.2.9.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.9.RELEASE</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

This chain pulls in Spring MVC, Tomcat, and related libraries, enabling you to use MVC features out of the box.

What Is a Configuration File?

Spring Boot uses application.properties (or application.yml) to externalize configuration. Each starter defines a set of @ConfigurationProperties classes that bind properties with a specific prefix, such as spring.mvc for MVC settings. To customize, simply add entries like spring.mvc.xxx=yyy to the properties file.

What Is the Main (Boot) Class?

The generated DemoApplication class looks like this:

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

The @SpringBootApplication annotation is a meta‑annotation that combines @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan. These enable automatic configuration, import starter beans, and scan the package hierarchy for components.

How to Write Unit Tests?

Spring Boot creates a test class scaffolded with @SpringBootTest:

@SpringBootTest
class DemoApplicationTests {
    @Test
    void contextLoads() {
    }
}

You can inject beans directly, for example:

@SpringBootTest
class DemoApplicationTests {
    @Autowired
    private HelloWorldController helloWorldController;
}

While useful for integration testing, such tests can increase build time for large projects.

Conclusion

This article provides a quick, hands‑on introduction to Spring Boot: its purpose, project creation via Spring Initializr, a basic Hello World REST endpoint, starter dependency mechanics, property configuration, the role of the main application class, and a simple unit‑test example.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend Developmentmavenunit testingSpring Bootrest
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.