DEV Community

Cover image for How to Build and Deploy a REST API Using Spring Boot 3.x
AI Development Company
AI Development Company

Posted on

How to Build and Deploy a REST API Using Spring Boot 3.x

Introduction

In the era of microservices and cloud-native applications, REST APIs serve as the backbone of communication between distributed systems. Among various backend frameworks, Spring Boot has emerged as a leading choice due to its simplicity, flexibility, and production-readiness. With the release of Spring Boot 3.x, building and deploying RESTful APIs has become even more streamlined and developer-friendly. In this blog, we’ll walk through a practical guide to building and deploying a REST API using Spring Boot 3.x, covering setup, development, testing, and deployment.

Why Spring Boot 3.x?

Spring Boot 3.x is a major release that builds on the robust features of its predecessors while introducing new improvements such as:

  • Java 17+ baseline support
  • Support for Jakarta EE 10
  • Native image support with GraalVM
  • Enhanced observability via Micrometer and Micrometer Tracing
  • Improved compatibility with Spring Framework 6

These advancements make Spring Boot 3.x ideal for modern application development and deployment in containerized and cloud environments.

Step 1: Setting Up Your Spring Boot Project

To kickstart your REST API development:

Navigate to Spring Initializr: https://start.spring.io/

Choose the following options:

Project: Maven

Language: Java

Spring Boot: 3.x.x (latest)

Dependencies: Spring Web, Spring Boot DevTools, Spring Data JPA, H2 Database

Click Generate and unzip the downloaded file.

You can also use your IDE (like IntelliJ IDEA or Eclipse) to import the project directly.

Step 2: Define Your Application Structure

Let’s assume we’re building a simple API for managing books in a library. Here's a typical folder structure:

src
 └─ main
     ├─ java
     │   └─ com.example.library
     │       ├─ controller
     │       ├─ model
     │       ├─ repository
     │       └─ service
     └─ resources
         ├─ application.properties
Enter fullscreen mode Exit fullscreen mode

Step 3: Create Your Model

package com.example.library.model;

import jakarta.persistence.*;

@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String title;
    private String author;
    private String isbn;

    // Getters and Setters
}

Enter fullscreen mode Exit fullscreen mode

Step 4: Create Repository Layer

package com.example.library.repository;

import com.example.library.model.Book;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BookRepository extends JpaRepository<Book, Long> {
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Implement Service Layer

package com.example.library.service;

import com.example.library.model.Book;
import com.example.library.repository.BookRepository;
import org.springframework.stereotype.Service;

import java.util.List;

Enter fullscreen mode Exit fullscreen mode
@Service
public class BookService {
    private final BookRepository bookRepository;

    public BookService(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

    public List<Book> getAllBooks() {
        return bookRepository.findAll();
    }

    public Book saveBook(Book book) {
        return bookRepository.save(book);
    }

    public void deleteBook(Long id) {
        bookRepository.deleteById(id);
    }
}

Enter fullscreen mode Exit fullscreen mode

Step 6: Develop Controller Layer

`package com.example.library.controller;

  • import com.example.library.model.Book;
  • import com.example.library.service.BookService;
  • import org.springframework.http.HttpStatus;
  • import org.springframework.http.ResponseEntity;
  • import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/books")
public class BookController {
private final BookService bookService;

public BookController(BookService bookService) {
    this.bookService = bookService;
}

@GetMapping
public ResponseEntity<List<Book>> getBooks() {
    return new ResponseEntity<>(bookService.getAllBooks(), HttpStatus.OK);
}

@PostMapping
public ResponseEntity<Book> createBook(@RequestBody Book book) {
    return new ResponseEntity<>(bookService.saveBook(book), HttpStatus.CREATED);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteBook(@PathVariable Long id) {
    bookService.deleteBook(id);
    return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
Enter fullscreen mode Exit fullscreen mode

}
`
Step 7: Configure the Application

In application.properties:

  • spring.datasource.url=jdbc:h2:mem:testdb
  • spring.datasource.driverClassName=org.h2.Driver
  • spring.datasource.username=sa
  • spring.datasource.password=
  • spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
  • spring.h2.console.enabled=true Step 8: Test Your API

Run the application (LibraryApplication.java) and access the endpoints using Postman or curl:

  • GET /api/books — List all books
  • POST /api/books — Add a new book
  • DELETE /api/books/{id} — Delete a book
    Step 9: Build the Project

  • Use Maven to build your Spring Boot JAR:

  • ./mvnw clean package

  • You’ll get a JAR file inside the target folder:

  • target/library-0.0.1-SNAPSHOT.jar

Step 10: Deploy Your Spring Boot REST API

You have multiple deployment options:

1. Run Locally

java -jar target/library-0.0.1-SNAPSHOT.jar

2. Deploy to Docker

Create a Dockerfile:

  • FROM eclipse-temurin:17-jdk
  • ARG JAR_FILE=target/*.jar
  • COPY ${JAR_FILE} app.jar
  • ENTRYPOINT ["java","-jar","/app.jar"] Build and run the image:

docker build -t springboot-library .
docker run -p 8080:8080 springboot-library

3. Deploy to Cloud (Heroku / AWS / Azure / GCP)

Most cloud providers offer Spring Boot-friendly environments. You can deploy using:

  • Heroku CLI: git push heroku main
  • AWS Elastic Beanstalk
  • Azure App Service
  • Google Cloud App Engine
  • Best Practices for REST APIs in Spring Boot
  • Use DTOs (Data Transfer Objects) for payloads
  • Validate input using javax.validation
  • Use Swagger/OpenAPI for API documentation
  • Secure endpoints with Spring Security
  • Implement global exception handling with @ControllerAdvice

Conclusion

Spring Boot 3.x simplifies REST API development by providing an opinionated, yet highly customizable framework for Java developers. From setting up the project to deploying it to the cloud or Docker, this guide has walked through the essential steps to build a production-ready REST API. Whether you're creating microservices or monoliths, Spring Boot remains a reliable choice with extensive community and enterprise support.

If you're looking to accelerate your backend development, consider partnering with a spring boot development company that offers comprehensive spring boot development services. Whether you’re a startup building your MVP or an enterprise seeking to modernize your stack, you can also Hire springboot developer to scale your project efficiently with expert guidance.

Top comments (0)