6

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;
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
user2868835
  • 1,250
  • 3
  • 19
  • 33
  • You can use [`.toEpochMilli`](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#toEpochMilli--) which gets a long value. Then returning it back to Instant is more easier with `.ofEpochMilli` – KarelG Jul 06 '17 at 10:04
  • 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? – user2868835 Jul 06 '17 at 10:16
  • `java.time.Instant` has ISO-8601 as string representation, a swift look into the [API doc](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#toString--) 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. – specializt Jul 06 '17 at 11:03
  • I've tried adding @JsonFormat but the json being generated doesn't recognise ths... – user2868835 Jul 06 '17 at 11:25

3 Answers3

4

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")
user2868835
  • 1,250
  • 3
  • 19
  • 33
  • 1
    There is a better way - add the following to your application.properties: `spring.jackson.serialization.write_dates_as_timestamps=false` – MatanRubin May 17 '18 at 07:28
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/

Armali
  • 18,255
  • 14
  • 57
  • 171
Steve Nash
  • 144
  • 6
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