0

How should I exchange DateTime objects between Spring and Retrofit?

At this moment I have timestamp in MySQL, Instant in JPA entities (because JPA Buddy uses Instant by default) and Spring produces YYYY-MM-DDTHH:mm:ss.sssZ string in JSON, but it looks like Retrofit is not able to deserialize it into Instant.

This is example of my Spring response:

[
    {
        "id": 1,
        "code": "Abc",
        "name": "Wiha PH3",
        "description": null,
        "created": "2022-10-10T12:00:00Z",
        "modified": "2022-10-10T12:00:00Z",
    }
]

At this moment I had to change my Retrofit DTO datetime fields from Instant to String because it was throwing exceptions about getting string instead of objects.

@Keep
data class InventoryItemDto(
    var id: Int,
    var code: String,
    var name: String,
    var description: String,
    var created: String,         // this is datetime!
    var modified: String         // this is datetime!
)

I have seen similar question here: https://stackoverflow.com/a/34983454/1215291

but in this new project I can change data types in each layer, so maybe there is no need to mess around with deserializers or changing date time format in Spring?

Kamil
  • 13,363
  • 24
  • 88
  • 183

0 Answers0