Spring and Spring Boot are both part of the Spring Framework, but they serve different purposes. Let’s compare them first and then go through a hands-on Spring Boot tutorial.
Table of Contents
🔹 Spring vs Spring Boot: Key Differences
Feature | Spring Framework | Spring Boot |
---|---|---|
Definition | A comprehensive framework for Java EE applications. | A simplified way to build Spring applications with minimal setup. |
Configuration | Requires extensive XML or Java-based configuration. | Uses auto-configuration, reducing boilerplate code. |
Dependency Management | Manually managed via pom.xml or gradle.build . | Uses Spring Boot Starters for simplified dependencies. |
Web Server | Requires external Tomcat, Jetty, or Undertow. | Comes with an embedded Tomcat, Jetty, or Undertow. |
Microservices Support | Needs additional setup for REST APIs & Microservices. | Optimized for microservices architecture. |
Ease of Development | More complex and requires manual setup. | Faster development with minimal configuration. |
👉 Bottom Line: Spring Boot is an extension of Spring that makes it easier to create and deploy Spring-based applications.
🚀 Hands-On Spring Boot Tutorial: Building a REST API
Step 1️⃣: Set Up a Spring Boot Project
You can create a new Spring Boot project using Spring Initializr:
- Open Spring Initializr.
- Select:
- Project: Maven or Gradle
- Language: Java
- Spring Boot Version: Latest stable
- Dependencies:
Spring Web
- Click Generate to download the project.
Step 2️⃣: Add REST Controller
Navigate to src/main/java/com/example/demo/
and create a new file:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}
📌 Explanation:
@RestController
– Marks this class as a REST API controller.@RequestMapping("/api")
– Defines the base URL path.@GetMapping("/hello")
– Maps HTTP GET requests to/api/hello
.
Step 3️⃣: Run the Spring Boot Application
In your terminal, navigate to the project folder and run:
mvn spring-boot:run
OR
gradle bootRun
Step 4️⃣: Test the API
Open a browser or use Postman and go to:
http://localhost:8080/api/hello
📌 You should see: “Hello, Spring Boot!” 🎉
🎯 Why Choose Spring Boot?
✔ Fast Development – No need to set up an external server.
✔ Auto Configuration – Reduces boilerplate code.
✔ Production-Ready – Built-in monitoring and logging with Spring Actuator.
✔ Great for Microservices – Works seamlessly with Spring Cloud.
Read other awesome articles in Medium.com or in akcoding’s posts.
OR
Join us on YouTube Channel
OR Scan the QR Code to Directly open the Channel 👉
