How to return Java 'java.time.Instant' property as json value in Restful API body response?
Asked Answered
N

3

7

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;
}
Nonconformity answered 6/7, 2017 at 10:1 Comment(4)
You can use .toEpochMilli which gets a long value. Then returning it back to Instant is more easier with .ofEpochMilliDyson
Reading further on this, the general sentiment seems to be to have all json timestamp properties returned using the 8601 format. Is use of the '@jsonformat' annotation the right way to serve this?Nonconformity
java.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
I've tried adding @JsonFormat but the json being generated doesn't recognise ths...Nonconformity
N
5

Solved it... I was mising theh jsr310 maven dependency

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

On clarification... if I want all Instant properties to be returned in json as UTC should i use the following format instruction, or is there another better way of doing this...

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSSZ",timezone = "UTC")
Nonconformity answered 6/7, 2017 at 12:21 Comment(1)
There is a better way - add the following to your application.properties: spring.jackson.serialization.write_dates_as_timestamps=falseTitivate
L
4
  1. pom.xml add

    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
    </dependency>
    
  2. application.properties add

    spring.jackson.serialization.write-dates-as-timestamps=false
    

link https://codeboje.de/jackson-java-8-datetime-handling/

Loire answered 22/1, 2019 at 12:58 Comment(0)
L
1

You can simply use the java.time.Instant to their representation using .toString() If you are using Spring, you might have to add @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) to your RestMethod in order to make sure it is converted correctly

Leading answered 6/7, 2017 at 11:25 Comment(2)
I can't get this to work. Whatever annotation I put in the json being returned comes across as ... "validUntil": { "epochSecond": 1499772556, "nano": 656000000 } .Nonconformity
I'm using Spring Boot 1.5.4Nonconformity

© 2022 - 2024 — McMap. All rights reserved.