Spring Boot & H2 - 更新記錄



現在讓我們更新迄今為止建立的專案,以準備一個完整的更新記錄 API 並對其進行測試。

更新控制器

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

員工控制器

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 details
   @PutMapping("/employee")
   public void updateEmployee(@RequestBody Employee employee) {
      employeeService.saveOrUpdate(employee);
   }	
}

執行應用程式

在 Eclipse 中,執行在 應用程式設定 期間準備的“員工應用程式”配置。

Eclipse 控制檯將顯示類似的輸出。

[INFO] Scanning for projects...
...
2024-08-21T15:01:29.377+05:30  INFO 14180 --- [springboot-h2] [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2024-08-21T15:01:29.426+05:30  INFO 14180 --- [springboot-h2] [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2024-08-21T15:01:29.434+05:30  INFO 14180 --- [springboot-h2] [  restartedMain] c.t.s.SpringbootH2Application            : Started SpringbootH2Application in 6.729 seconds (process running for 7.28)

伺服器啟動並執行後,使用 Postman 發出 POST 請求 -

在 POSTMAN 中設定以下引數。

  • HTTP 方法 - POST

  • URL - https://:8080/emp/employee

  • 主體 - 一個員工 JSON

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

點擊發送按鈕並檢查響應狀態是否為 OK。

Add Employee

現在發出 PUT 請求以更新這些記錄。

在 POSTMAN 中設定以下引數。

  • HTTP 方法 - PUT

  • URL - https://:8080/emp/employee

  • 主體 - 一個員工 JSON

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

點擊發送按鈕並驗證響應狀態是否為 OK。

Update Employee

現在發出 GET 請求以獲取該記錄。

在 POSTMAN 中設定以下引數。

  • HTTP 方法 - GET

  • URL - https://:8080/emp/employee/1

點擊發送按鈕並驗證響應。

Get Updated Employee
廣告

© . All rights reserved.