I have a Spring Boot restful API service that returns a Java object in its response which is translated into json.
One of the Java object properties is a 'Java.time.Instant'. How should I translate this for the json object being returned?
update
I've tried using @JsonFormat but this doesn't work...
The Java object being returned has an 'Instant' property...
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSSZ",timezone = "UTC")
public Instant getRequested() {
return Requested;
}
This is coming back in the json response body as...
"requested": {
"epochSecond": 1499342121,
"nano": 868000000
},
I'm using Spring Boot 1.5.4
The controller method is...
@RequestMapping(value="/", method= RequestMethod.POST)
public AcceptedAccountRequest newRequest(@RequestBody NewAccountRequest aRequest) {
AcceptedAccountRequest anAcceptedRequest = createAccepted(aRequest);
return anAcceptedRequest;
}
.toEpochMilli
which gets a long value. Then returning it back to Instant is more easier with.ofEpochMilli
– Dysonjava.time.Instant
has ISO-8601 as string representation, a swift look into the API doc also would've revealed this - so as long as you're not using some weird, reflection-based serialization you're good to go. Please use google. – Bolshevik