- Spring Boot & H2 教程
- Spring Boot & H2 - 首页
- Spring Boot & H2 - 概述
- Spring Boot & H2 - 环境设置
- Spring Boot & H2 - 项目设置
- Spring Boot & H2 - REST API
- Spring Boot & H2 - H2 控制台
- Spring Boot & H2 示例
- Spring Boot & H2 - 添加记录
- Spring Boot & H2 - 获取记录
- Spring Boot & H2 - 获取所有记录
- Spring Boot & H2 - 更新记录
- Spring Boot & H2 - 删除记录
- Spring Boot & H2 - 控制器单元测试
- Spring Boot & H2 - 服务单元测试
- Spring Boot & H2 - 仓库单元测试
- Spring Boot & H2 有用资源
- Spring Boot & H2 - 快速指南
- Spring Boot & H2 - 有用资源
- Spring Boot & H2 - 讨论
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。
现在发出 PUT 请求以更新这些记录。
在 POSTMAN 中设置以下参数。
HTTP 方法 - PUT
URL - https://:8080/emp/employee
主体 - 一个员工 JSON
{
"id": "1",
"age": "35",
"name": "Julie",
"email": "julie.roberts@gmail.com"
}
点击发送按钮并验证响应状态是否为 OK。
现在发出 GET 请求以获取该记录。
在 POSTMAN 中设置以下参数。
HTTP 方法 - GET
URL - https://:8080/emp/employee/1
点击发送按钮并验证响应。
广告