Spring Boot & Microservices - Day3
Spring Boot & Microservices - Day3
Pre-requisites
- Java
- Spring Framework
Software requirements
- JDK 8 or later
- Eclipse / STS
- MySQL
- Postman Client
Libraries
1. Web
2. Devtools
How to run spring boot applications
1. From machine you can launch the application using IDE
2. You can create an executable jar file and run using java -jar
command
@GetMapping(path = “/balance”)
public String fetchBalance() { … }
@PutMapping(path = “/update”)
public String updateBalance() { .. }
}
Get /myapi/balance calls fetchBalance
Put /myapi/update calls updateBalance
Postman client: It is an application used to test the webservices.
Writing a simple webservice & test from the postman
@RestController
@RequestMapping(“/myapi”)
public class MyResource {
@GetMapping
public ResponseEntity greet() {
How to read JSON data of the Request and map to the Java object
in Webservice
@PostMapping(path = “/store”, consumes =
MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> method (@RequestBody
Employee emp) {
// Spring Boot maps each json property to the Employee object
properties automatically
// you can use that object to pass to the Service layer
}
How to read data coming from the URL parameter
We must parameterize the path in HTTP methods
@GetMapping(“{id}”) and extract that value using
@PathVariable(“id”), if you send /200, then the
@GetMapping(“{id}”) will have value 100 in it &
@PathVariable(“id”) will extract that value 100.
@PutMapping(“{id}/{phone}”), now it matches to the url
/100/9292393, and we must have @PathVariable(“id”) &
@PathVariable(“phone”) to extract these values
Things to create
1. Employee class with id, name & salary and setters, getters
2. Webservice that can accept JSON & map to the Employee
object
3. Webservice that can accept path parameters.
Layered architecture
Controller layer: Takes care of processing the request &
generating the response, it accesses service layer
Service layer: Takes care of implementing business logic, it
accesses DAO layer
DAO layer: Takes care of implementing database operations i.e.,
CRUD operations
Dependency of objects
Controller depends on Service, and service depends on DAO layer,
in spring boot we can make use of Dependency injection feature
and let spring to supply the respective dependencies
Benefits of Spring Data JPA
1. You get interfaces that provides all the CRUD operations
a. CrudRepository<T, ID>
b. JpaRepository<T, ID>
2. You don’t have to implement the DAO layer, spring boot
takes care of implementing the DAO layer based on the
interface you create
3. You don’t have to write code to establish connection, instead
configure database properties in application.properties,
spring boot takes care of establishing connection
4. Uses ORM framework so that your entity class can be
mapped to the table
CrudRepository provides methods like save(), delete(), findAll()
methods
JpaRepository extends CrudRepository and provides some extra
methods for sorting & pagination
Steps
1. Add below libraries in your project
a. Spring Data JPA
b. MySQL Driver
2. Create entity class that maps to the employee table, mark
primary key property
3. Structure the project to have separate packages for separate
layers like controller, service, dao and etc.
4. Create a repository interface that extends either
CrudRepository or JpaRepository (this gives spring boot an
idea how to implement the repository)
Note: Spring Boot provides an auto-implementation &
registers that object in the spring container
5. Create a service layer that uses repository interface to
perform operations
a. We must create an interface in Service layer that is
used by the clients (Controller)
b. We must implement that interface to provide
implementation for all the methods of the interface
6. Controller must call the service layer methods
7. Configure application.properties to have datasource
information’s