Published on Sat Apr 12 2025 11:00:00 GMT+0000 (Coordinated Universal Time) by Purusothaman Ramanujam
Getting Started with Spring Boot: Your First REST API
Introduction
Spring Boot is a powerful framework that makes it easy to create stand-alone, production-grade Spring-based applications. It takes care of most configurations, allowing you to focus on writing business logic. One of the most common uses of Spring Boot is to build RESTful APIs quickly and efficiently.
In this post, you’ll learn how to set up a simple Spring Boot project and create your first REST API endpoint.
What is Spring Boot?
Spring Boot is an extension of the Spring framework that simplifies the setup and development of new Spring applications. It provides:
- Auto-configuration
- Embedded servers (Tomcat, Jetty, etc.)
- Production-ready features (metrics, health checks)
- Minimal configuration
Setting Up Your Project
The easiest way to start is with Spring Initializr:
- Go to https://start.spring.io/
- Choose:
- Project: Maven or Gradle
- Language: Java
- Spring Boot: 3.x.x (latest)
- Group:
com.example
- Artifact:
demo
- Dependencies:
Spring Web
- Click Generate to download the project zip.
- Unzip and open in your favorite IDE (IntelliJ IDEA, Eclipse, VS Code).
Project Structure
Your project will look like this:
src/ main/ java/ com/ example/ demo/ DemoApplication.java
Writing Your First REST Controller
Let’s create a simple REST controller that returns a greeting message.
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;
@RestControllerpublic class HelloController { @GetMapping("/hello") public String sayHello(@RequestParam(defaultValue = "World") String name) { return "Hello, " + name + "!"; }}
@RestController
tells Spring this class handles HTTP requests.@GetMapping("/hello")
maps GET requests to/hello
.@RequestParam
binds thename
query parameter (default: “World”).
Running the Application
- In your IDE, run the
DemoApplication
class (it has amain
method). - By default, the app runs on http://localhost:8080.
- Open your browser or use curl/Postman:
You should see:
Hello, World!
Or:
Hello, Java!
Adding More Endpoints
Let’s add a simple API to manage a list of books in memory.
package com.example.demo;
import org.springframework.web.bind.annotation.*;import java.util.*;
@RestController@RequestMapping("/books")public class BookController { private List<Book> books = new ArrayList<>();
@GetMapping public List<Book> getAllBooks() { return books; }
@PostMapping public Book addBook(@RequestBody Book book) { books.add(book); return book; }}
class Book { private String title; private String author;
// Getters and setters public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; }}
@RequestMapping("/books")
sets the base path.@GetMapping
returns all books.@PostMapping
adds a new book (send JSON in the request body).
Best Practices
- Use DTOs: For real projects, use Data Transfer Objects (DTOs) instead of exposing entities directly.
- Validation: Add validation to your request bodies.
- Service Layer: Separate business logic into service classes.
- Exception Handling: Use
@ControllerAdvice
for global error handling. - Persistence: Use Spring Data JPA for database access.
Conclusion
Spring Boot makes it easy to create REST APIs with minimal setup. In this post, you learned how to create a simple REST controller and add endpoints. As you grow more comfortable, explore features like validation, database integration, and security.
Resources
Written by Purusothaman Ramanujam
← Back to blog