- Spring Boot & H2 - Home
- Spring Boot & H2 - Overview
- Spring Boot & H2 - Environment Setup
- Spring Boot & H2 - Project Setup
- Spring Boot & H2 - REST APIs
- Spring Boot & H2 - H2 Console
Spring Boot & H2 Examples
- Spring Boot & H2 - Add Record
- Spring Boot & H2 - Get Record
- Spring Boot & H2 - Get All Records
- Spring Boot & H2 - Update Record
- Spring Boot & H2 - Delete Record
- Spring Boot & H2 - Unit Test Controller
- Spring Boot & H2 - Unit Test Service
- Spring Boot & H2 - Unit Test Repository
Spring Boot & H2 Useful Resources
Spring Boot & H2 - Delete Record
Let's now update the project created so far to prepare a complete Delete Record API and test it.
Update Service
// Use repository.deleteById() to delete an Employee record
public void deleteEmployeeById(int id) {
repository.deleteById(id);
}
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
public class EmployeeService {
@Autowired
EmployeeRepository repository;
// get an employee by id
public Employee getEmployeeById(int id) {
return repository.findById(id).get();
}
// get all employees
public List<Employee> getAllEmployees(){
List<Employee> employees = new ArrayList<Employee>();
repository.findAll().forEach(employee -> employees.add(employee));
return employees;
}
// create or update an employee
public void saveOrUpdate(Employee employee) {
repository.save(employee);
}
// delete an employee by id
public void deleteEmployeeById(int id) {
repository.deleteById(id);
}
}
Update Controller
// Use service.deleteEmployeeById() to delete an employee by id
@DeleteMapping("/employee/{id}")
public void deleteEmployee(@PathVariable("id") int id) {
employeeService.deleteEmployeeById(id);
}
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")
public class EmployeeController {
@Autowired
EmployeeService employeeService;
// get all employees
@GetMapping("/employees")
public List<Employee> getAllEmployees(){
return employeeService.getAllEmployees();
}
// get an employee by id
@GetMapping("/employee/{id}")
public Employee getEmployee(@PathVariable("id") int id) {
return employeeService.getEmployeeById(id);
}
// delete an employee by id
@DeleteMapping("/employee/{id}")
public void deleteEmployee(@PathVariable("id") int id) {
employeeService.deleteEmployeeById(id);
}
// create an employee
@PostMapping("/employee")
public void addEmployee(@RequestBody Employee employee) {
employeeService.saveOrUpdate(employee);
}
// update an employee
@PutMapping("/employee")
public void updateEmployee(@RequestBody Employee employee) {
employeeService.saveOrUpdate(employee);
}
}
Run the application
In eclipse, run the Employee Application configuration as prepared during Application Setup
Eclipse console will show the similar output.
[INFO] Scanning for projects... ... 2025-10-01T20:55:14.722+05:30 INFO 35360 --- [springboot-h2] [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2025-10-01T20:55:14.730+05:30 INFO 35360 --- [springboot-h2] [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/' 2025-10-01T20:55:14.730+05:30 INFO 35360 --- [springboot-h2] [ restartedMain] c.t.s.SpringbootH2Application : Started SpringbootH2Application in 0.357 seconds (process running for 17063.522) 2025-10-01T20:55:14.734+05:30 INFO 35360 --- [springboot-h2] [ restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged
Once server is up and running, Use Postman to make a POST request −
Set the following parameters in POSTMAN.
HTTP Method − POST
URL − http://localhost:8080/emp/employee
BODY − An employee JSON
{
"id": "1",
"age": "35",
"name": "Julie",
"email": "julie@gmail.com"
}
Click on Send Button and check the response status to be OK.
Now make a Delete Request to delete that records.
Set the following parameters in POSTMAN.
HTTP Method − DELETE
URL − http://localhost:8080/emp/employee/1
Click the send button and verify 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
Click the send button and verify the response.