Reputation: 13
I need help to create an endpoint that receives a "timestamp" (complete date with time) and save it to the database.
Upvotes: 0
Views: 1806
Reputation: 173
An oversimplified example of receiving a MySQL timestamp into a java.sql.Timestamp
object using a spring-boot @RestController
would look something like this (I tried to work in as many Timestamp examples as possible so please forgive the poor API design):
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
}
}
This example assumes that clients of the RESTful API, be it humans or other systems, are passing validly formatted MySQL timestamps to it.
The model object I used for this example looks like this:
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.
}
Request for POST:
curl --location --request POST 'http://localhost:8080/resource' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Rubber Duck"
}'
Response for POST:
{
"id": "1",
"name": "Rubber Duck",
"createdOnTimestamp": "2022-01-27T21:45:32.064+00:00",
"lastUpdatedTimestamp": "2022-01-27T21:45:32.064+00:00"
}
Request for 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'
Response for GET:
[
{
"id": "1",
"name": "Rubber Duck",
"createdOnTimestamp": "2022-01-27T21:45:32.064+00:00",
"lastUpdatedTimestamp": "2022-01-27T21:45:32.064+00:00"
}
]
Request for 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"
}'
Response for PUT:
{
"id": "1",
"name": "Plastic Goose",
"createdOnTimestamp": null,
"lastUpdatedTimestamp": "2022-01-27T21:50:00.000+00:00"
}
Request for PATCH:
curl --location --request PATCH 'http://localhost:8080/resource/1?newTimestampToStore=2022-01-27 16:00:00.000' \
--header 'Content-Type: application/json'
Response for PATCH:
{
"id": "1",
"name": "Plastic Goose",
"createdOnTimestamp": "2022-01-27T21:48:03.627+00:00",
"lastUpdatedTimestamp": "2022-01-27T22:00:00.000+00:00"
}
Upvotes: 1