Mike
Mike

Reputation: 832

How to use java.time.Instant with Swagger 2.x?

I have a REST endpoint in my java webapp that accepts an Instant as a query parameter as seen in the code below

@GET
@Produces(MediaType.APPLICATION_JSON)
Response getHistory(
    @QueryParam("beforeTime")
    Instant beforeTime,

    @QueryParam("afterTime")
    Instant afterTime) {
    ...
}

This worked well under swagger 1.x, but when we switch to swagger 2.x the objects of Instant are showing up as $int64 instead of a date-time field. What can I do with the swagger annotations to get this to work?

Upvotes: 0

Views: 1364

Answers (1)

Mike
Mike

Reputation: 832

I was able to use the @ApiParam annotation to fix this issue. Setting the type to date-time and format to java.time.Instant fixed the issue.

@GET
@Produces(MediaType.APPLICATION_JSON)
Response getHistory(
    @QueryParam("beforeTime")
    @ApiParam(type="date-time", format = "java.time.Instant")
    Instant beforeTime,

    @QueryParam("afterTime")
    @ApiParam(type="date-time", format = "java.time.Instant")
    Instant afterTime) {
    ...
}

Upvotes: 1

Related Questions