Microservice – Book Exchange Service using Spring Boot

What you will learn here about Microservices

  • Microservice – Book Exchange Service using Spring Boot


Please Start Naming server Before starting the Book exchange service. Please visit following link for knowing how to implement naming server in microservice

Eureka naming server in microservices

Please click on following link to download Book exchange service project

Download Book exchange Service project (645 downloads)

Microservice – Book Exchange Service using Spring Boot

Please follow the following steps to know to how to make simple microservice with inbuit H2 database.
1)First create a simple maven project

2)Add following dependency in pom.xml which is shown below

Spring boot starter dependency


Spring data Jpa dependency


H2 database dependency


Eureka client discovery dependency


Dev tools dependency


netflix eureka client dependency

3)Add following properties in application.properties which is shown below




h2 database properties

4)Create Entiry class with respective fields which is shown below

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

public class Book {
	private int id;
	private String book_Name;
	private String author;
	private String description;
	private double price;
	private String environment;
	public double getPrice() {
		return price;
	public void setPrice(double price) {
		this.price = price;
	public String getEnvironment() {
		return environment;
	public void setEnvironment(String string) {
		this.environment = string;
	public int getId() {
		return id;
	public void setId(int id) {
		this.id = id;
	public String getBook_Name() {
		return book_Name;
	public void setBook_Name(String book_Name) {
		this.book_Name = book_Name;
	public String getAuthor() {
		return author;
	public void setAuthor(String author) {
		this.author = author;
	public String getDescription() {
		return description;
	public void setDescription(String description) {
		this.description = description;

microservice with h2 database

5)Create Jpa repository for Entity class which is shown below

import org.springframework.data.jpa.repository.JpaRepository;

public interface BooksRepository extends JpaRepository<Book, Integer>{


spring data jpa repository

6)Create data.sql under resources to insert data into H2 database which is shown below

insert into book(id, author, book_name, description, price) 
	values(1000,'Robert keyosaki','Rich Dad Poor Dad','Financial advice',300.50);
insert into book(id, author, book_name, description, price) 
	values(1001,'Joseph murph','Power of subconcious mind','Personality development',270.00);	

insert data into h2 database

7)Create rest controller which is shown below

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

public class BookExchangeController {
	BooksRepository repository;
	Environment environment;
	public Book getBook(@PathVariable int id) {
		Book book=repository.findById(id).get();
		return book;

spring boot find book service

8)Now please start the your bookexchangeservice

9)Now go to browser and visit localhost:8000/book/id/1001 which is shown below
find book service spring boot

You may also like...