Spring Boot with Docker
Introduction
This blog will show how to integrate Spring Data with Spring Boot. The flow will be from the Rest Controller which will wire the service to make call to JPA Repository as shown below.
Maven Dependency
Initialize the Spring Boot Application using start.spring.io with Web and JPA dependency added to it.
Need to add below dependency to support Spring Data JPA and embedded h2 database
- spring-boot-starter-data-jpa [ org.springframework.boot ]
- h2 [ com.h2database ]
Repository Changes
Define an interface that extends JpaRepository for the entity along with the primary key. Here, we have an Employee Entity and the primary key data type for the entity is String.
Service Changes
Inject the Repo as dependency in Service Layer and make use of findAll() method and getById method to return the list of employees or return a specific employee
Entity Changes
Define a model, that maps entity bean to a table.
Note : Please add @JsonIgnoreProperties to override below exception
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serialiser found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor
Starter Changes
Need to add below annotation
- @EnableJpaRepositories
- @EnableEntityScan
SQL Changes
Place the SQL files in main/resources folder to load it into DB , on application startup
Config Changes
H2 Console Dashboard
We can able to access the H2 dashboard @ localhost:8095/h2-console ( Here the port no specified in application.properties should match )
And the JDBC URL should be : jdbc:h2:mem:testdb
Then click on test connection, to see its successful.
Testing
Test the endpoint through Postman
Happy Learning !
Bharathy Poovalingam