【发布时间】:2022-01-27 19:44:23
【问题描述】:
我需要帮助来创建一个接收“时间戳”(带时间的完整日期)的端点并将其保存到数据库中。
【问题讨论】:
标签: java mysql spring-boot endpoint
我需要帮助来创建一个接收“时间戳”(带时间的完整日期)的端点并将其保存到数据库中。
【问题讨论】:
标签: java mysql spring-boot endpoint
使用 spring-boot @RestController 将 MySQL 时间戳接收到 java.sql.Timestamp 对象的过度简化示例看起来像这样(我尝试在尽可能多的 Timestamp 示例中工作,因此请原谅糟糕的 API 设计) :
package myresourceapp.controllers;
import myresourceapp.models.Resource;
import myresourceapp.services.ResourceService;
import org.springframework.web.bind.annotation.*;
import java.sql.Timestamp;
import java.util.List;
@RestController
public class ResourceController {
private final ResourceService resourceService; //service layer bean to either do DAO stuff or wrap DAO layer
public ResourceController(ResourceService resourceService) { //dependency injection
this.resourceService = resourceService;
}
@PostMapping("/resource")
public @ResponseBody
Resource createResource(@RequestBody Resource resource) {
return resourceService.insertResource(resource); //<-- service method for eventual INSERT query
}
@GetMapping("/resources")
public @ResponseBody
List<Resource> getResources(@RequestParam Timestamp startTimestamp, @RequestParam Timestamp endTimestamp) {
return resourceService.selectResourcesCreatedBetweenStartAndEndTimestamps(startTimestamp, endTimestamp); //<-- service method for eventual SELECT query
}
@PutMapping("/resource/{resourceId}")
public @ResponseBody Resource updateResource(@PathVariable String resourceId, @RequestBody Resource resource) {
return resourceService.updateResource(resourceId, resource); //<-- service method for eventual UPDATE query
}
@PatchMapping("/resource/{resourceId}")
public @ResponseBody Resource updateTimestamp(@PathVariable String resourceId, @RequestParam Timestamp newTimestampToStore) {
return resourceService.updateResourceLastUpdatedTimestamp(resourceId, newTimestampToStore); //<-- service method for eventual UPDATE query
}
}
此示例假设 RESTful API 的客户端,无论是人类还是其他系统,都将有效格式化的 MySQL 时间戳传递给它。
我在本例中使用的模型对象如下所示:
package myresourceapp.models.Resource;
import java.sql.Timestamp;
public class Resource {
private String id;
private String name;
private Timestamp createdOnTimestamp;
private Timestamp lastUpdatedTimestamp;
//getters, setters, etc.
}
POST 请求:
curl --location --request POST 'http://localhost:8080/resource' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Rubber Duck"
}'
对 POST 的响应:
{
"id": "1",
"name": "Rubber Duck",
"createdOnTimestamp": "2022-01-27T21:45:32.064+00:00",
"lastUpdatedTimestamp": "2022-01-27T21:45:32.064+00:00"
}
请求 GET:
curl --location --request GET 'http://localhost:8080/resources?startTimestamp=2022-01-01 12:00:00.000&endTimestamp=2022-01-31 12:00:00.000' \
--header 'Content-Type: application/json'
GET 响应:
[
{
"id": "1",
"name": "Rubber Duck",
"createdOnTimestamp": "2022-01-27T21:45:32.064+00:00",
"lastUpdatedTimestamp": "2022-01-27T21:45:32.064+00:00"
}
]
PUT 请求:
curl --location --request PUT 'http://localhost:8080/resource/1' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Plastic Goose",
"lastUpdatedTimestamp": "2022-01-27T21:50:00.000+00:00"
}'
对 PUT 的响应:
{
"id": "1",
"name": "Plastic Goose",
"createdOnTimestamp": null,
"lastUpdatedTimestamp": "2022-01-27T21:50:00.000+00:00"
}
申请补丁:
curl --location --request PATCH 'http://localhost:8080/resource/1?newTimestampToStore=2022-01-27 16:00:00.000' \
--header 'Content-Type: application/json'
对 PATCH 的回应:
{
"id": "1",
"name": "Plastic Goose",
"createdOnTimestamp": "2022-01-27T21:48:03.627+00:00",
"lastUpdatedTimestamp": "2022-01-27T22:00:00.000+00:00"
}
【讨论】: