Spring Boot is a framework built on top of the Spring Framework. Its main goal is to simplify Spring-based application development.
Starter dependencies: Pre-packaged Maven/Gradle dependencies for common use cases (like spring-boot-starter-web for web apps, spring-boot-starter-data-jpa for databases).
Auto-configuration: Spring Boot automatically configures the application based on classpath libraries and beans.
Embedded server: Runs with embedded Tomcat/Jetty, so no external server setup is needed.
Spring Boot CLI: Command-line tool to run and test Spring Boot apps quickly.
Actuator: Provides endpoints for monitoring, health checks, metrics, and environment info.
Externalized configuration: Supports .properties or .yaml files, environment variables, and command-line args.
Opinionated defaults: Provides sensible defaults but allows overriding if needed. Offers pre-configured setups for common tasks (web apps, REST APIs, database access)
Java & Maven
👉 Visit: https://start.spring.io
👉 Choose:
Project: Maven
Language: Java
Spring Boot Version: (latest stable, e.g., 3.3.x)
Packaging: Jar
Java Version: 17
👉 Add dependencies:
Spring Web (for REST APIs)
Spring Boot DevTools (for auto-restart in dev)
Spring Data JPA (for databases, optional)
H2 Database (for in-memory testing)
👉 Click Generate — it downloads a .zip
👉 Unzip it and open it in your IDE. That’s the new Spring Boot project scaffold — ready to run.
In IntelliJ → New Project → Spring Initializr
myapp/
├─ src/
│ ├─ main/
│ │ ├─ java/com/example/myapp/
│ │ │ └─ MyAppApplication.java ← main entry point
│ │ └─ resources/
│ │ ├─ application.properties ← configuration file (externalized)
│ │ └─ static/ ← static assets (optional)
| | └─ templates/ ← HTML templates (optional)
│ └─ test/ ← for unit tests
├─ pom.xml ← dependencies + build config
package com.example.myapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // Enables auto-configuration & component scanning
public class MyAppApplication {
public static void main(String[] args) {
SpringApplication.run(MyAppApplication.class, args);
}
}
SpringApplication.run(MyAppApplication.class, args); → starts the Spring ApplicationContext — the central container that manages all the application Spring beans, configurations, and dependency injection.
The @SpringBootApplication annotation is a combination of three annotations:
@Configuration – Marks the class as a source of bean definitions
@EnableAutoConfiguration – Tells Spring Boot to configure beans automatically
@ComponentScan – Scans for Spring components in the package
Detects libraries on the classpath (like Spring Web, Spring Data JPA, etc.)
Automatically configures required beans (like DispatcherServlet for REST APIs)
Applies default configurations
Runs Tomcat/Jetty/Undertow
Just like any Java application, Spring Boot apps need a class with a public static void main(String[] args) method, which tells JVM where to start execution
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}
@RestController = @Controller + @ResponseBody
@Controller− Marks the class as a Spring MVC controller, responsible for handling incoming HTTP requests.
@ResponseBody− Tells Spring to serialize the return value of the controller's method directly into the HTTP response body, typically in formats like JSON or XML.
Spring Boot auto-configures Jackson for JSON serialization/deserialization:
public class User {
private String name;
private int age;
// required for JSON deserialization
public User() {}
// getters & setters
}
@RestController
public class UserController {
@GetMapping("/user")
public User getUser() {
return new User("Alice", 30); // automatically converted to JSON
}
}
In src/main/resources/application.properties:
server.port=9090
spring.application.name=myapp
That changes the port to 9090.
To connect to a database like PostgreSQL or MySQL, just add a dependency in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
And configure in application.properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=secret
spring.jpa.hibernate.ddl-auto=update
Directly from the IDE
Using: mvn spring-boot:run
👉 Option 1: As a JAR (standalone)
Spring Boot apps package everything (code + dependencies + embedded server) into a single executable JAR.
Build it with: mvn clean package
Then run it directly: java -jar target/myapp-0.0.1-SNAPSHOT.jar
👉 Option 2: As a WAR (deployable to external Tomcat/JBoss)
Configure Spring Boot to produce a WAR instead of a JAR, and deploy it to an external application server.
👉 Option 3: In Docker
Create a Dockerfile
In the project root:
# Use an official OpenJDK image as the base
FROM eclipse-temurin:17-jdk
# Includes: A full JDK, Linux base system (Ubuntu or Debian), Security updates & system libraries
# One of the most common and recommended base images for Java apps today (development & production)
# Set working directory
WORKDIR /app
# Copy the JAR file (after building with Maven)
COPY target/myapp-0.0.1-SNAPSHOT.jar app.jar
# Run the app
ENTRYPOINT ["java", "-jar", "app.jar"]
Build the JAR: mvn clean package
Then build the image: docker build -t myapp:latest .
Run the container: docker run -p 8080:9090 myapp:latest
Host machine: 8080 → What you access from browser or API client
Inside container: 9090 → What Spring Boot is listening on (from server.port)
Then you can open: 👉 http://localhost:8080/hello
Note: If the app connects to a database, then use Doctor compose (docker-compose.yml) or Kubernetes
Once the app is containerized, it can be deployed it to:
Kubernetes (K8s)
AWS ECS / EKS
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/users") // Base path for all routes
public class UserController {
// Example: GET http://localhost:8080/users
@GetMapping
public Collection<User> getAllUsers() {
}
// GET - Using @PathVariable
// Example: GET http://localhost:8080/users/1
@GetMapping("/{id}")
public User getUserById(@PathVariable int id) {
}
// GET - Using @RequestParam (query parameter)
// Example: GET http://localhost:8080/users/search?name=Alice
@GetMapping("/search")
public List<User> searchUserByName(@RequestParam String name) {
}
// POST - Create new user with JSON body
// Example: POST http://localhost:8080/users
// Body: { "name": "Charlie", "age": 22 }
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public String createUser(@RequestBody User newUser) {
}
// PUT - Full update (replace user data)
// Example: PUT http://localhost:8080/users/2
// Body: { "name": "Robert", "age": 26 }
@PutMapping("/{id}")
public String updateUser(@PathVariable int id, @RequestBody User updatedUser) {
}
// PATCH - Partial update (change only name)
// Example: PATCH http://localhost:8080/users/1?name=Alice%20Smith
@PatchMapping("/{id}")
public String updateUserName(@PathVariable int id, @RequestParam String name) {
}
// DELETE - Remove user
// Example: DELETE http://localhost:8080/users/2
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteUser(@PathVariable int id) {
}
// GET - Using @RequestHeader
// Example: GET http://localhost:8080/users/header
// Header: User-Agent: MyCustomClient
@GetMapping("/header")
public String getHeaderExample(@RequestHeader("User-Agent") String userAgent) {
return "Your User-Agent is: " + userAgent;
}
// GET - Trigger custom exception example
// Example: GET http://localhost:8080/users/error
@GetMapping("/error")
public User triggerError() {
throw new UserNotFoundException("Demo: user not found");
}
// Custom exception with @ResponseStatus
@ResponseStatus(HttpStatus.NOT_FOUND)
static class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String msg) {
super(msg);
}
}
}