Skip to content

Introduction to Spring Boot

Building Your First Hello World Application

Introduction

Java enterprise development traditionally involved complex configuration and extensive boilerplate code. Spring Boot transforms this landscape by offering a streamlined, convention-over-configuration approach that has revolutionized how we build Java applications. As modern applications demand rapid development and deployment cycles, understanding Spring Boot becomes crucial for any Java developer.

The Spring Boot Journey

Spring Boot emerged from the challenges developers faced with the traditional Spring Framework. While Spring offered powerful dependency injection and enterprise features, it required extensive XML configuration and setup time. Spring Boot addresses these pain points through intelligent defaults and automatic configuration.

Think of Spring Boot as a master chef who has prepared all ingredients and tools before you start cooking. You don’t need to specify how to mix basic ingredients – those decisions are made for you based on best practices. Yet, you retain the flexibility to adjust any aspect when needed.

Understanding the Magic

The heart of Spring Boot lies in its opinionated approach. When you include a database dependency, Spring Boot automatically configures the connection pool, transaction manager, and JPA settings. This “magic” comes from Spring Boot’s auto-configuration, which analyzes your classpath and configures components based on common use cases.

Learning Objectives:

  • Set up a Spring Boot project using Spring Initializer
  • Understand basic project structure and dependencies
  • Create and understand the role of a REST controller
  • Implement and test a simple HTTP endpoint

Let’s begin by creating our project using Spring Initializer (start.spring.io). This web-based tool is the recommended way to bootstrap Spring Boot projects.

Spring Initializer webpage with the following settings highlighted

Navigate to https://start.spring.io and configure your project with these settings:

  1. Project settings:
    • Project: Maven
    • Language: Java
    • Spring Boot: 3.4.0
    • Packaging: Jar
    • Java Version: 17
  2. Project Metadata:
    • Group: academy.javapro
    • Artifact: demo
    • Name: demo
    • Description: First Spring Boot Application
    • Package name: academy.javapro.demo
  3. Dependencies:
    • Click on “ADD DEPENDENCIES” and add:
      • Spring Web (provides REST endpoints and Tomcat server)
      • Spring Boot DevTools (optional, for development convenience)
Selected dependencies in Spring Initializer

Click “GENERATE” to download the project as a ZIP file. Extract it to your workspace directory.

The Spring Initializer has created a complete project structure for us:

javapro-hello-world/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main/
β”‚   β”‚   β”œβ”€β”€ java/
β”‚   β”‚   β”‚   └── academy/
β”‚   β”‚   β”‚       └── javapro/
β”‚   β”‚   β”‚           └── HelloWorldApplication.java
β”‚   β”‚   └── resources/
β”‚   β”‚       β”œβ”€β”€ static/
β”‚   β”‚       β”œβ”€β”€ templates/
β”‚   β”‚       └── application.properties
β”‚   └── test/
β”‚       └── java/
β”œβ”€β”€ .gitignore
β”œβ”€β”€ mvnw
β”œβ”€β”€ mvnw.cmd
└── pom.xml

The generated pom.xml already includes our required 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 https://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>3.4.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>academy.javapro</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>17</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-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</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>

Now that we have our project structure, let’s create our controller package and Hello World endpoint. Create a new package called controller under src/main/java/academy/javapro/:

package academy.javapro.demo.controller;

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

@RestController
public class HelloWorldController {
    
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, JavaPro Academy!";
    }
}

The main application class was generated for us:

package academy.javapro.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

Let’s break down the key components of our application:

  1. @SpringBootApplication: This powerful annotation combines:
    • @Configuration: Marks the class as a source of bean definitions
    • @EnableAutoConfiguration: Enables Spring Boot’s auto-configuration mechanism
    • @ComponentScan: Tells Spring to scan for components in the current package and sub-packages
  2. @RestController: A combination of:
    • @Controller: Marks the class as a web controller
    • @ResponseBody: Indicates that method return values should be bound to the web response body
  3. @GetMapping("/hello"): Maps HTTP GET requests to the specified path
    • Part of Spring’s powerful request mapping system
    • Supports various HTTP methods (GET, POST, PUT, DELETE, etc.)

To run the application:

  1. Import the project into your IDE (IntelliJ IDEA, Eclipse, or VS Code)
  2. Wait for Maven to download all dependencies
  3. Run the HelloWorldApplication class
  4. Open your browser and navigate to: http://localhost:8080/hello
IDE showing project successfully imported with all dependencies

You should see the message “Hello, JavaPro Academy!” displayed in your browser.

Application running in browser with response visible

Understanding What We Built

Our simple Hello World application demonstrates several Spring Boot features:

  1. Auto-configuration: Spring Boot automatically configured:
    • An embedded Tomcat server
    • Appropriate JSON message converters
    • Basic error handling
  2. Dependency Management: The parent POM provides:
    • Compatible dependency versions
    • Proper plugin configurations
    • Spring Boot-specific build features
  3. Development Tools:
    • Spring Boot DevTools enables automatic restart
    • Built-in error handling and debugging support
    • Easy access to logs and metrics

Common Questions and Solutions

Q: Why use Spring Boot instead of traditional Spring?
A: Spring Boot reduces boilerplate, provides sensible defaults, and includes an embedded server.

Q: How can I change the server port?
A: Add server.port=8081 to application.properties

Q: What if I need custom configuration?
A: Spring Boot allows overriding any default through properties or Java configuration.

    Next Steps

    In our next lessons, we’ll explore:

    • Building RESTful APIs with multiple endpoints
    • Handling different HTTP methods (POST, PUT, DELETE)
    • Working with request parameters and path variables
    • Implementing basic error handling
    • Connecting to databases
    • Adding security

    Great job, everyone! You’ve made excellent progress in understanding Spring Boot basics. Remember to take breaks and give your mind time to process this new information. We’ll see you in the next lecture where we’ll build upon these concepts. Keep up the great work!

    Remember to:

    • Practice creating new endpoints
    • Explore Spring Boot’s auto-configuration properties
    • Read the Spring Boot documentation for deeper understanding
    • Experiment with different annotations and configurations

    The Spring Initializer approach we used offers several advantages:

    1. Generates a complete, properly structured project
    2. Includes necessary Maven/Gradle configuration
    3. Adds appropriate dependency management
    4. Creates a .gitignore file with common exclusions
    5. Includes test directory structure and basic test class

    Keep exploring and experimenting with Spring Boot. The foundation you’ve built today will serve you well as you develop more complex applications in the future.

    program to practice these concepts. Understanding these fundamentals will enable you to tackle more advanced Java topics, such as loops, methods, and object-oriented programming, with confidence.


    Transform your future with Java Pro Academy’s comprehensive Spring Boot course! Our Java Full Stack Developer program takes you from foundational concepts to advanced enterprise applications, guided by industry veterans who understand what employers need. Master essential skills through hands-on projects that simulate real-world development scenarios, diving deep into data structures, algorithms, and Spring Boot architecture.

    Whether you’re starting your coding journey or advancing your existing skills, our intensive Java Bootcamp delivers practical expertise in modern full-stack development. Build a powerful portfolio while learning alongside passionate developers in our collaborative environment. Join Spring Boot Academy today and launch your career as a professional Java Full Stack Developer with confidence.

    Join the conversation

    Your email address will not be published. Required fields are marked *