Reputation: 396
I am hoping to get some help debugging this problem. If I send the following JSON to my backend it works correctly:
{
"approvalRequired": false,
"location": {
"locationName": "<+37.33233141,-122.03121860> +\/- 5.00m (speed 0.00 mps \/ course -1.00) @ 9\/16\/18, 9:24:59 PM Pacific Daylight Time",
"longitude": -122.0312186,
"latitude": 37.332331410000002
}
}
However, if I now send the following:
{
"approvalRequired": false,
"scheduledStartTime": "2016-01-01T10:24:00+01:00",
"location": {
"locationName": "<+37.33233141,-122.03121860> +\/- 5.00m (speed 0.00 mps \/ course -1.00) @ 9\/16\/18, 9:24:59 PM Pacific Daylight Time",
"longitude": -122.0312186,
"latitude": 37.332331410000002
}
}
I get the above error. In my backend code I have the following:
@DynamoDBTypeConverted(converter = ZonedDateTimeTypeConverter.class)
@DynamoDBAttribute(attributeName = "scheduledStartTime")
public ZonedDateTime scheduledStartTime;
And the API method signature looks like this:
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity create(@RequestBody Event event) {...}
I believe the problem I am having is that the JSON cannot be parsed to ZonedDateTime. Does anyone have advice as to either, (1) what time of json string format ZonedDateTime automatically accepts or (2) how to make a DTO to parse zoned date time?
Thanks!
Upvotes: 9
Views: 631
Reputation: 2773
Send in this format 2016-08-22T14:30+08:00[Asia/Kuala_Lumpur]
LocalDateTime ldt = LocalDateTime.of(2016, Month.AUGUST, 22, 14, 30);
ZonedDateTime klDateTime = ldt.atZone(ZoneId.of("Asia/Kuala_Lumpur"));
Upvotes: 0
Reputation: 1383
Assume you are using the most "default" configuration, which is based on FasterXML Jackson.
If so, then you just need to configure properly serializer and desirializer for ZonedDateTime
in your application; and it might be either custom ones or the ones from jackson-datatype-jsr310 (recommended).
I've created a small/minimal example, which is based on the Spring 5.0.9 and Jackson 2.9.6 (latest versions currently).
Please find it here: spring5-rest-zoneddatetime >>, main parts are:
Event
DTO:
public class Event {
private long id;
private String name;
private ZonedDateTime time;
// Constructors, public getters and setters
}
Field time
might be a public
one same to your sample, it is also fine, but if field is private
- then you will need public
getter and setter.
NOTE: I'm ignoring here @DynamoDBTypeConverted
and @DynamoDBAttribute
annotations since they are related to persistence logic, not the REST layer.
EventController
contains only one method same to yours:
@RestController
public class EventController {
@RequestMapping(value = "/event", method = RequestMethod.POST)
public ResponseEntity post(@RequestBody Event event) {
System.out.println("Event posted: " + event.toString());
return ResponseEntity.ok(event);
}
}
Dependencies in the pom.xml
looks so:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.6</version>
</dependency>
The important one here is JSR-310 datatype implementation, which also introduces com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer
and com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer
.
In case of custom serializer/desirializer will be needed, please check this question >>
Next date formats will be supported for the time
field:
"2018-01-01T22:25:15+01:00[Europe/Paris]"
- not fully an ISO 8601 btw"2018-01-01T22:25:15+01:00"
"2018-01-01T22:25:15.000000001Z"
1514768461.000000001
- float-pointing number, amount of seconds from the 1970-01-01, 00:00:00 [UTC]
By default REST APi response will use float-pointing numbers for dates, e.g. in our case response will look so:
{
"id": 3,
"name": "Test event",
"time": 1514768460
}
To return string values instead, please check e.g. this question >>
Also need to mention that if you will use Spring Boot instead (good starter) - all things discussed above will work out of the box.
Upvotes: 3