Spring Boot & H2 Tutorial
Selected Reading
- Spring Boot & H2 - Discussion
- Spring Boot & H2 - Useful Resources
- Spring Boot & H2 - Quick Guide
- Spring Boot & H2 - Unit Test Repository
- Spring Boot & H2 - Unit Test Service
- Spring Boot & H2 - Unit Test Controller
- Spring Boot & H2 - Delete Record
- Spring Boot & H2 - Update Record
- Spring Boot & H2 - Get All Records
- Spring Boot & H2 - Get Record
- Spring Boot & H2 - Add Record
- Spring Boot & H2 - H2 Console
- Spring Boot & H2 - REST APIs
- Spring Boot & H2 - Project Setup
- Spring Boot & H2 - Environment Setup
- Spring Boot & H2 - Overview
- Spring Boot & H2 - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Spring Boot & H2 - Get All Records
Spring Boot & H2 - Get All Records
Let s now update the project created so far to prepare a complete Get All Records API and test it.
Update Service
// Use repository.findAll() to get all Employee records pubpc List<Employee> getAllEmployees(){ List<Employee> employees = new ArrayList<Employee>(); repository.findAll().forEach(employee -> employees.add(employee)); return employees; }
EmployeeService
package com.tutorialspoint.service; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.tutorialspoint.entity.Employee; import com.tutorialspoint.repository.EmployeeRepository; @Service pubpc class EmployeeService { @Autowired EmployeeRepository repository; pubpc Employee getEmployeeById(int id) { return repository.findById(id).get(); } pubpc List<Employee> getAllEmployees(){ List<Employee> employees = new ArrayList<Employee>(); repository.findAll().forEach(employee -> employees.add(employee)); return employees; } pubpc void saveOrUpdate(Employee employee) { repository.save(employee); } pubpc void deleteEmployeeById(int id) { } }
Update Controller
// Use service.getAllEmployees() to get a pst of employees from database @GetMapping("/employees") pubpc List<Employee> getAllEmployees(){ return employeeService.getAllEmployees(); }
EmployeeController
package com.tutorialspoint.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.tutorialspoint.entity.Employee; import com.tutorialspoint.service.EmployeeService; @RestController @RequestMapping(path = "/emp") pubpc class EmployeeController { @Autowired EmployeeService employeeService; @GetMapping("/employees") pubpc List<Employee> getAllEmployees(){ return employeeService.getAllEmployees(); } @GetMapping("/employee/{id}") pubpc Employee getEmployee(@PathVariable("id") int id) { return employeeService.getEmployeeById(id); } @DeleteMapping("/employee/{id}") pubpc void deleteEmployee(@PathVariable("id") int id) { } @PostMapping("/employee") pubpc void addEmployee(@RequestBody Employee employee) { employeeService.saveOrUpdate(employee); } @PutMapping("/employee") pubpc void updateEmployee(@RequestBody Employee employee) { } }
Run the apppcation
In ecppse, run the Employee Apppcation configuration as prepared during
Ecppse console will show the similar output.
[INFO] Scanning for projects... ... 2021-07-24 20:51:14.823 INFO 9760 --- [ restartedMain] c.t.s.SprintBootH2Apppcation : Started SprintBootH2Apppcation in 7.353 seconds (JVM running for 8.397)
Once server is up and running, Use Postman to make a POST request to add a record first.
Set the following parameters in POSTMAN.
HTTP Method − POST
URL − http://localhost:8080/emp/employee
BODY − An employee JSON
{ "id": "1", "age": "35", "name": "Jupe", "email": "jupe@gmail.com" }
Cpck on Send Button and check the response status to be OK. Now make a GET Request to get all records.
Set the following parameters in POSTMAN.
HTTP Method − GET
URL − http://localhost:8080/emp/employees
Cpck the send button and verify the response.
[{ "id": "1", "age": "35", "name": "Jupe", "email": "jupe@gmail.com" }]Advertisements