Everything Java

← Back to blog

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:

Setting Up Your Project

The easiest way to start is with Spring Initializr:

  1. Go to https://start.spring.io/
  2. Choose:
    • Project: Maven or Gradle
    • Language: Java
    • Spring Boot: 3.x.x (latest)
    • Group: com.example
    • Artifact: demo
    • Dependencies: Spring Web
  3. Click Generate to download the project zip.
  4. 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;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello(@RequestParam(defaultValue = "World") String name) {
return "Hello, " + name + "!";
}
}

Running the Application

  1. In your IDE, run the DemoApplication class (it has a main method).
  2. By default, the app runs on http://localhost:8080.
  3. 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; }
}

Best Practices

  1. Use DTOs: For real projects, use Data Transfer Objects (DTOs) instead of exposing entities directly.
  2. Validation: Add validation to your request bodies.
  3. Service Layer: Separate business logic into service classes.
  4. Exception Handling: Use @ControllerAdvice for global error handling.
  5. 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