Spring Boot & H2 - Update Record


Advertisements

Let's now update the project created so far to prepare a complete Update Record API and test it.

Update Controller

// Use service.saveOrUpdate() to update an employee record
@PutMapping("/employee")
public void updateEmployee(@RequestBody Employee employee) {
   employeeService.saveOrUpdate(employee);
}	

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;
   @GetMapping("/employees")
   public List<Employee> getAllEmployees(){
      return employeeService.getAllEmployees();
   }
   @GetMapping("/employee/{id}")
   public Employee getEmployee(@PathVariable("id") int id) {
      return employeeService.getEmployeeById(id);
   }
   @DeleteMapping("/employee/{id}")
   public void deleteEmployee(@PathVariable("id") int id) {
      employeeService.deleteEmployeeById(id);
   }
   @PostMapping("/employee")
   public void addEmployee(@RequestBody Employee employee) {
      employeeService.saveOrUpdate(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...
...
2021-07-24 20:51:14.823  INFO 9760 --- [  restartedMain] c.t.s.SprintBootH2Application
: Started SprintBootH2Application 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": "Julie",  
   "email": "julie@gmail.com"  
}   

Click on Send Button and check the response status to be OK.

Now make a Put Request to update that records.

Set the following parameters in POSTMAN.

  • HTTP Method − PUT

  • URL − http://localhost:8080/emp/employee

  • BODY − An employee JSON

{  
   "id": "1",  
   "age": "35",  
   "name": "Julie",  
   "email": "julie.roberts@gmail.com"  
}   

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.

[{  
   "id": "1",  
   "age": "35",  
   "name": "Julie",  
   "email": "julie.roberts@gmail.com"  
}]   
Advertisements