Skip to content

Create a microservices-based application with two services: BookService and AuthorService. The BookService will use OpenFeign to interact with the AuthorService to retrieve author information for books.

Notifications You must be signed in to change notification settings

Sujith-Sam098/books_author_open_feign_practice

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Case Study: Book and Author Management System

Objective: Create a microservices-based application with two services: BookService and AuthorService. The BookService will use OpenFeign to interact with the AuthorService to retrieve author information for books.

Overview

**BookService**: Manages book information and retrieves author details from AuthorService using OpenFeign.
**AuthorService**: Provides author details.

Architecture

BookService

Endpoints:
    /books: Retrieve a list of all books.
    /books/{id}: Retrieve details of a specific book, including author information.

AuthorService

Endpoints:
    /authors: Retrieve a list of all authors.
    /authors/{id}: Retrieve details of a specific author.

Step-by-Step Implementation

Set Up the Projects

Create a Spring Boot project using Spring Initializr for both BookService and AuthorService. Include the following dependencies:

Spring Web
Lombok
OpenFeign (Needed only for BookService)

Implement the AuthorService

Author model class for AuthorService:

public class Author {
    private Long id;
    private String name;
    // Getters and setters
}

AuthorRepository for AuthorService:

@Repository
public class AuthorRepository {

    private List<Author> authors = new ArrayList<>();

    public AuthorRepository() {
        authors.add(new Author(1L, "George Orwell"));
        authors.add(new Author(2L, "J.K. Rowling"));
        authors.add(new Author(3L, "Harper Lee"));
        authors.add(new Author(4L, "J.R.R. Tolkien"));
        authors.add(new Author(5L, "Jane Austen"));
    }

    public List<Author> findAll() {
    }

    public Author findById(Long id) {
    }
}

AuthorController for AuthorService:

@RestController
public class AuthorController {

    @Autowired
    private AuthorRepository authorRepository;

    @GetMapping("/authors")
    public List<Author> getAllAuthors() {
        return authorRepository.findAll();
    }

    @GetMapping("/authors/{id}")
    public Author getAuthorById(@PathVariable Long id) {
        return authorRepository.findById(id);
    }
}

Implement the BookService

Book model class for BookService:

public class Book {
    private Long id;
    private String title;
    private Long authorId;
    // Getters and setters
}

BookRepository for BookService:

@Repository
public class BookRepository {

    private List<Book> books = new ArrayList<>();

    public BookRepository() {
        books.add(new Book(1L, "1984", 1L));
        books.add(new Book(2L, "Harry Potter", 2L));
        books.add(new Book(3L, "To Kill a Mockingbird", 3L));
        books.add(new Book(4L, "The Lord of the Rings", 4L));
        books.add(new Book(5L, "Pride and Prejudice", 5L));
    }

    public List<Book> findAll() {
    }

    public Book findById(Long id) {
    }
}

BookController for BookService:

@RestController
public class BookController {

    @Autowired
    private BookRepository bookRepository;

    @Autowired
    private AuthorClient authorClient;

    @GetMapping("/books")
    public List<BookDTO> getAllBooks() {
    }

    @GetMapping("/books/{id}")
    public BookDTO getBookById(@PathVariable Long id) {
    }
}

BookDTO class for BookService:

public class BookDTO {
    private Long id;
    private String title;
    private Author author;
    // Getters and setters
}

Implement the AuthorClient

AuthorClient will use OpenFeign to interact with the AuthorService:

public interface AuthorClient {
    public Author getAuthorById(@PathVariable Long id);
}

Tasks

  1. Fork the repository.
  2. Create AuhorService and BookService projects with the specified dependencies.
  3. Create the Author model class for AuthorService.
  4. Implement the AuthorRepository class for AuthorService.
  5. Implement the AuthorController class for AuthorService.
  6. Create the Book model class for BookService.
  7. Implement the BookRepository class for BookService.
  8. Implement the BookController class for BookService.
  9. Create the BookDTO class for BookService.
  10. Create the Author class for BookService which will be used in the BookDTO.
  11. Implement the AuthorClient interface for BookService.
  12. Implement the AuthorClient interface using OpenFeign.
  13. Update the BookController to use the AuthorClient to retrieve author information.
  14. Test the application.
  15. Implement error handling for cases where the author is not found.
  16. Push the code to GitHub.

Testing the Application

Get All Books: 
Access http://localhost:8080/books to get a list of all books with author details.

Get Book by ID: 
Access http://localhost:8080/books/1 to get details of a specific book, including author information.

About

Create a microservices-based application with two services: BookService and AuthorService. The BookService will use OpenFeign to interact with the AuthorService to retrieve author information for books.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%