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.
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.
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)
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);
}
}
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
}
AuthorClient
will use OpenFeign to interact with the AuthorService:
public interface AuthorClient {
public Author getAuthorById(@PathVariable Long id);
}
- Fork the repository.
- Create
AuhorService
andBookService
projects with the specified dependencies. - Create the
Author
model class for AuthorService. - Implement the
AuthorRepository
class for AuthorService. - Implement the
AuthorController
class for AuthorService. - Create the
Book
model class for BookService. - Implement the
BookRepository
class for BookService. - Implement the
BookController
class for BookService. - Create the
BookDTO
class for BookService. - Create the
Author
class for BookService which will be used in theBookDTO
. - Implement the
AuthorClient
interface for BookService. - Implement the
AuthorClient
interface using OpenFeign. - Update the
BookController
to use theAuthorClient
to retrieve author information. - Test the application.
- Implement error handling for cases where the author is not found.
- Push the code to GitHub.
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.