Spring Boot 3 Tutorial for Beginners: Build Your First REST API
Introduction
Spring Boot has become the most popular framework for building modern Java applications. It simplifies Java development by reducing configuration, providing embedded servers, and offering production-ready features out of the box.
In this tutorial, you will learn how to create your first Spring Boot 3 REST API step by step. By the end of this guide, you'll have a running web service that returns data through HTTP requests.
Whether you're a student, beginner Java developer, or someone transitioning from Servlets and JSP, this tutorial will help you get started with Spring Boot development.
What is Spring Boot?
Spring Boot is an open-source Java framework built on top of the Spring Framework. It simplifies application development by providing:
- Auto Configuration
- Embedded Tomcat Server
- Starter Dependencies
- Production-ready Features
- Simplified Project Setup
Instead of spending hours configuring XML files and application servers, developers can focus on writing business logic.
Benefits of Spring Boot
- Faster development
- Reduced configuration
- Easy dependency management
- Microservices support
- Cloud-ready applications
- Large community support
Prerequisites
Before starting, make sure you have:
- Java 17 or later installed
- Maven installed
- IntelliJ IDEA or Eclipse
- Basic Java knowledge
You can verify Java installation using:
java -version
Example output:
openjdk version "17.0.10"
Creating a Spring Boot Project
The easiest way to create a Spring Boot project is using Spring Initializr.
Project Configuration
Choose:
Project: Maven Language: Java Spring Boot: 3.x Group: com.javawebaction Artifact: springbootdemo Packaging: Jar Java: 17
Dependencies
Add:
Spring Web Spring Boot DevTools
Click Generate and download the project.
Open the project in your IDE.
Understanding the Project Structure
After importing the project, you'll see a structure similar to this:
springbootdemo ├── src │ ├── main │ │ ├── java │ │ └── resources │ └── test ├── pom.xml └── mvnw
Important Files
pom.xml
Contains project dependencies.
application.properties
Stores application configuration.
Main Application Class
This is the starting point of the application.
Example:
@SpringBootApplication public class SpringbootdemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootdemoApplication.class, args); } }
Creating Your First REST Controller
Create a new package:
com.javawebaction.controller
Inside the package, create:
package com.javawebaction.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class WelcomeController { @GetMapping("/") public String welcome() { return "Welcome to Java Web Action Spring Boot Tutorial"; } }
Explanation
@RestController
Marks the class as a REST Controller.
@GetMapping
Maps HTTP GET requests.
welcome()
Returns a simple response to the browser.
Running the Application
Run the main application class.
You should see output similar to:
Tomcat started on port(s): 8080 Started SpringbootdemoApplication
Spring Boot automatically starts an embedded Tomcat server.
No external server installation is required.
Testing the REST API
Open your browser and visit:
http://localhost:8080
Output:
Welcome to Java Web Action Spring Boot Tutorial
Congratulations!
You have successfully created your first Spring Boot REST API.
Returning JSON Response
REST APIs commonly return JSON data.
Update the controller:
package com.javawebaction.controller; import java.util.HashMap; import java.util.Map; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class WelcomeController { @GetMapping("/api") public Map<String, String> apiResponse() { Map<String, String> response = new HashMap<>(); response.put("status", "success"); response.put("message", "Spring Boot API is running"); return response; } }
Visit:
http://localhost:8080/api
Response:
{ "status": "success", "message": "Spring Boot API is running" }
Common Errors and Solutions
Port Already in Use
Error:
Port 8080 was already in use
Solution:
Change port in:
server.port=9090
Dependency Download Issues
Run:
mvn clean install
to download all dependencies.
Java Version Error
Ensure your project uses Java 17 or later.
Best Practices
When working with Spring Boot:
- Use meaningful package names
- Keep controllers lightweight
- Separate business logic into services
- Use DTOs for API responses
- Follow REST standards
- Use proper exception handling
Frequently Asked Questions (FAQ)
Is Spring Boot easier than Spring Framework?
Yes. Spring Boot removes much of the configuration required by traditional Spring applications.
Do I need Tomcat separately?
No. Spring Boot includes an embedded Tomcat server.
Which Java version should I use?
Spring Boot 3 officially supports Java 17 and newer versions.
Is Spring Boot used in real companies?
Yes. Many startups and enterprise organizations use Spring Boot for backend and microservices development.
Can Spring Boot be used for Microservices?
Absolutely. Spring Boot is one of the most popular frameworks for building microservices.
Conclusion
Spring Boot makes Java development faster, simpler, and more productive. In this tutorial, you learned how to create a Spring Boot project, build a REST controller, run an embedded server, and return both text and JSON responses.
This is just the beginning of your Spring Boot journey. In the next tutorial, we will create a more practical REST API and connect it with a database.
