Spring Boot Microservice Using Lombok

1 min read

Spring Boot Microservice Using Lombok

Writing clean and maintainable code is essential in any Java application, especially in microservices where modularity and simplicity matter most. The Lombok library is a powerful tool that helps Java developers reduce boilerplate code by automatically generating commonly used methods like getters, setters, constructors, and more. In this article, we’ll walk through how to build a simple Spring Boot microservice using Lombok to make your code more concise and readable — all while keeping things clean and production-ready.

Technologies Used:

  • Spring Boot (REST)
  • Lombok
  • Maven
  • Java 17+
  • In-memory List for simplicity (no database)

1. Project Structure

src/
└── main/
└── java/
└── com/example/bookservice/
├── BookServiceApplication.java
├── controller/
│ └── BookController.java
├── model/
│ └── Book.java
└── service/
└── BookService.java

2. Book.java (Model Layer)

package com.example.bookservice.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
    private String id;
    private String title;
    private String author;
}

What’s happening here?

  • @Data: generates getters, setters, toString(), equals(), and hashCode().
  • @NoArgsConstructor and @AllArgsConstructor: handle constructors.

3. BookService.java (Service Layer)

package com.example.bookservice.service;

import com.example.bookservice.model.Book;
import org.springframework.stereotype.Service;

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

@Service
public class BookService {

    private final List<Book> bookRepo = new ArrayList<>();

    public List<Book> getAllBooks() {
        return bookRepo;
    }

    public Book addBook(Book book) {
        bookRepo.add(book);
        return book;
    }
}

4. BookController.java (Controller Layer)

package com.example.bookservice.controller;

import com.example.bookservice.model.Book;
import com.example.bookservice.service.BookService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/books")
@RequiredArgsConstructor
public class BookController {

    private final BookService bookService;

    @GetMapping
    public List<Book> getBooks() {
        return bookService.getAllBooks();
    }

    @PostMapping
    public Book createBook(@RequestBody Book book) {
        return bookService.addBook(book);
    }
}

Explanation:

  • @RestController: simplifies REST API creation.
  • @RequiredArgsConstructor: auto-generates a constructor for final fields (like bookService).

5. Main Class

package com.example.bookservice;

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

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

Test with Curl or Postman

GET all books

curl http://localhost:8080/api/books

POST new book


🤞 Never miss a story from us, get weekly updates to your inbox!

Leave a Reply

Your email address will not be published. Required fields are marked *