Developer Community

Building RESTful APIs with Spring Boot

By Jane Doe Published: October 26, 2023 Category: Development Read Time: 8 min

Spring Boot has revolutionized the way Java developers build applications, and it's particularly adept at creating robust and scalable RESTful APIs. This post will guide you through the essential steps and concepts involved in building your first RESTful API using Spring Boot.

Why Spring Boot for REST APIs?

Spring Boot simplifies the development process by providing sensible defaults, auto-configuration, and embedded servers. This allows you to focus on your API's business logic rather than tedious configuration. Key benefits include:

Project Setup

The easiest way to start is by using Spring Initializr. Choose Maven or Gradle, your preferred Java version, and include the following dependencies:

Once generated, import the project into your IDE. Your main application class will look something like this:


package com.example.restapi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RestApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(RestApiApplication.class, args);
    }

}
            

Creating Your First Controller

A controller handles incoming HTTP requests. We'll create a simple `ProductController` to manage product resources.


package com.example.restapi.controller;

import com.example.restapi.model.Product;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/products") // Base path for all endpoints in this controller
public class ProductController {

    // In-memory storage for simplicity
    private List products = new ArrayList<>();
    private Long nextId = 1L;

    // GET all products
    @GetMapping
    public List<Product> getAllProducts() {
        return products;
    }

    // GET a product by ID
    @GetMapping("/{id}")
    public Optional<Product> getProductById(@PathVariable Long id) {
        return products.stream()
                       .filter(p -> p.getId().equals(id))
                       .findFirst();
    }

    // POST a new product
    @PostMapping
    public Product createProduct(@RequestBody Product product) {
        product.setId(nextId++);
        products.add(product);
        return product;
    }

    // PUT update a product
    @PutMapping("/{id}")
    public Optional<Product> updateProduct(@PathVariable Long id, @RequestBody Product updatedProduct) {
        Optional<Product> existingProduct = products.stream()
                                                     .filter(p -> p.getId().equals(id))
                                                     .findFirst();
        if (existingProduct.isPresent()) {
            int index = products.indexOf(existingProduct.get());
            updatedProduct.setId(id); // Ensure ID remains the same
            products.set(index, updatedProduct);
            return Optional.of(updatedProduct);
        }
        return Optional.empty();
    }

    // DELETE a product
    @DeleteMapping("/{id}")
    public void deleteProduct(@PathVariable Long id) {
        products.removeIf(p -> p.getId().equals(id));
    }
}
            

Model Class

We need a simple `Product` model class.


package com.example.restapi.model;

import java.math.BigDecimal;

public class Product {
    private Long id;
    private String name;
    private BigDecimal price;

    // Constructors
    public Product() {}

    public Product(Long id, String name, BigDecimal price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    // Getters and Setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
               "id=" + id +
               ", name='" + name + '\'' +
               ", price=" + price +
               '}';
    }
}
            

Key Annotations Explained

Running Your API

Start the application by running the `main` method in your `RestApiApplication` class. Your API will typically be available at http://localhost:8080. You can test your endpoints using tools like Postman, Insomnia, or cURL.

Next Steps

This is a basic introduction. To build production-ready APIs, consider:

Spring Boot makes building RESTful APIs a streamlined and enjoyable experience. Happy coding!