如何创建接收 "timestamp" 的端点

How to create an endpoint that receives a "timestamp"

我需要帮助来创建一个接收“时间戳”(带时间的完整日期)的端点并将其保存到数据库。

使用 spring-boot @RestController 将 MySQL 时间戳接收到 java.sql.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"
}