Bootstrap & First Controller
Walkthrough of initializing a Spring Boot project via Initializr, canonical directory layouts, @SpringBootApplication mechanics, REST controllers, @GetMapping routing, and step-by-step 404 debugging.
With our foundations in HTTP protocols, Servlets, and Maven build engineering, we are ready to create, configure, boot, and debug a production-ready Spring Boot Web Application.
1. Bootstrapping with Spring Initializr
Spring Initializr (start.spring.io) is the official project generator for the Spring ecosystem.
Initializr Selection Matrix:
├── Project Type : Maven Project
├── Language : Java
├── Spring Boot : 3.2.x (RELEASE)
├── Java Version : 17 or 21 (LTS releases)
└── Dependencies : Spring Web (spring-boot-starter-web)2. Standard Spring Boot Directory Structure
Spring Boot enforces standard Maven directory conventions:
my-spring-boot-app/
├── pom.xml # Maven configuration file
├── src/
│ ├── main/
│ │ ├── java/ # Java source code
│ │ │ └── com/kdev/demo/
│ │ │ ├── DemoApplication.java # Main Bootstrap Class
│ │ │ └── controller/
│ │ │ └── HelloController.java # REST Controller
│ │ └── resources/ # Non-Java application assets
│ │ ├── application.properties # Application configuration key-values
│ │ ├── static/ # Static web resources (HTML, CSS, JS)
│ │ └── templates/ # Server-side templates (Thymeleaf)
│ └── test/
│ └── java/ # Unit and integration test suites
│ └── com/kdev/demo/
│ └── DemoApplicationTests.java| Directory | Purpose |
|---|---|
src/main/java | Contains production Java source code. Package hierarchies start here. |
src/main/resources | Contains application properties, database migrations, static web assets. |
application.properties | Central key-value file for configuring port numbers, database URLs, logging levels. |
3. The Main Application Class: @SpringBootApplication
Every Spring Boot application requires a single bootstrap entry point containing a main method:
package com.kdev.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
// Bootstraps Spring IoC Container & launches Embedded Tomcat
SpringApplication.run(DemoApplication.class, args);
}
}Deconstructing @SpringBootApplication
@SpringBootApplication is a meta-annotation that combines three core Spring annotations:
[!IMPORTANT] Component Scanning Package Rule:
@ComponentScanscans for components in the package containingDemoApplicationAND all its sub-packages. IfDemoApplicationis incom.kdev.demo, Spring scanscom.kdev.demo.controller,com.kdev.demo.service, etc. If a bean is placed outside incom.other.stuff, Spring will ignore it!
4. Writing Our First REST Controller
A REST Controller is a Spring-managed component that handles HTTP requests and returns data payloads (usually JSON) directly to the client.
package com.kdev.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
// Maps HTTP GET requests for "/api/greet" to this method
@GetMapping("/api/greet")
public String greetUser(@RequestParam(defaultValue = "Guest") String name) {
return "Hello, " + name + "! Welcome to Spring Boot.";
}
}Key Annotations Explained
@RestController: Combines@Controllerand@ResponseBody. It tells Spring that method return values should be serialized directly into the HTTP response body (as text or JSON), rather than rendering an HTML template page.@GetMapping("/api/greet"): Shortcut annotation mapping incoming HTTPGETrequests for the path/api/greetto the annotated method.
5. End-to-End Request Journey in Spring Boot
What actually happens when a user requests http://localhost:8080/api/greet?name=Kamran in their browser?
- Tomcat Acceptor: Embedded Tomcat receives TCP packet on port
8080. - DispatcherServlet: Tomcat delegates request to Spring MVC's central front controller,
DispatcherServlet. - Handler Mapping:
DispatcherServletqueriesHandlerMappingto match/api/greetwith@GetMapping("/api/greet")inHelloController. - Execution: Spring injects parameters (
name="Kamran") and executesgreetUser(). - Serialization: The returned
Stringis serialized byHttpMessageConverterinto the HTTP response body.
6. Application Configuration: application.properties
You can customize runtime settings inside src/main/resources/application.properties:
# Server Port Configuration
server.port=8081
# Application Identity
spring.application.name=demo-service
# Logging Customization
logging.level.org.springframework.web=DEBUGIf you set server.port=8081, launching the app causes Tomcat to listen on port 8081 instead of 8080.
7. Debugging Checklist: Resolving Common Startup & 404 Errors
Pitfall 1: 404 Not Found Error
HTTP Status 404 – Not Found
Type: Status Report
Message: No static resource api/greet.- Cause A (Wrong Package Location):
HelloControllerwas placed in packagecom.kdev.outsidewhileDemoApplicationis incom.kdev.demo.@ComponentScanmissed it.- Fix: Move
HelloControllerinto packagecom.kdev.demo.controller.
- Fix: Move
- Cause B (Missing
@RestController): Used@Controllerinstead of@RestControllerwithout adding@ResponseBody. Spring tried to find a HTML view template namedapi/greet.html.
Pitfall 2: PortAlreadyInUseException (WebServerException)
APPLICATION FAILED TO START
Description:
Web server failed to start. Port 8080 was already in use.- Cause: Another process (e.g. another running instance of Tomcat or Docker container) is already bound to port 8080.
- Fix: Change port in
application.properties(server.port=8082) or terminate the conflicting process.
❓ Knowledge Check
What will happen if a developer places a @RestController in package 'com.company.util' when the @SpringBootApplication class is in package 'com.company.app'?
What is the functional difference between @Controller and @RestController in Spring Boot?
Maven Build Engineering
Deep dive into Apache Maven build pipelines, pom.xml architecture, GAV coordinates, SNAPSHOT vs Release semantics, transitive dependency trees, local .m2 caching, and spring-boot-maven-plugin mechanics.
Inversion of Control & Dependency Injection
Architectural mechanics of IoC and DI, tight vs loose coupling, interface abstraction, constructor vs field injection, SRP/OCP compliance, and unit testability.