javaspring-bootutcrest-assuredjava.time.instant

How can I convert an Instant during a Restassured request?


I want to perform a Restassured GET request. The response contains a list of objects which in turn have an Instant property.

Currently I get an exception:

com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.time.Instant from String "2022-03-08T20:53:02.990": Failed to deserialize java.time.Instant: (java.time.format.DateTimeParseException) Text '2022-03-08T20:53:02.990' could not be parsed at index 19 at [Source: (String)"{"timestamp":"bf105ae0-9f21-11ec-9c3e-fddae3040d6b","time":"2022-03-08T20:53:02.990","user":"XXX_SYSTEM","correlationId":"8fc84c87-aece-45b8-a7a6-66317152c840","key":"20220308_A14_0000000000000_7777777700003_5743e8cd40554a7d8110aa149e7015de_53","category":"INFO","context":"BLA","type":"BLUBB","system":true,"event":"FOO_BAR"}"; line: 1, column: 67] (through reference chain: xx.yyy.zzz.dto.MyResult$MyResultBuilder["time"])

The request:

        return given()
        .baseUri(baseUrl)
        .pathParam("aaa", value)
        .param("zeitstempelAb", FORMATTER.format(from))
        .param("zeitstempelBis", FORMATTER.format(to))
        .param("limit", "1000")
        .when()
        .log().all()
        .get("aaa/{value}")
        .then()
        .log().all()
        .assertThat()
        .statusCode(200)
        .extract()
        .body()
        .jsonPath()
        .getList("content", MyResult.class)
        ;

The used formatter:

    private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
    .appendPattern("yyyy-MM-dd'T'HH:mm:ss".concat("Z"))
    .toFormatter()
    .withZone(ZoneOffset.UTC);

The MyResult POJO:

@JsonDeserialize(builder = MyResult.MyResultBuilder.class)
@Value
@Builder(setterPrefix = "with")
public class MyResult {

    UUID timestamp;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'", timezone = "UTC")
    Instant time;
    .
    .
    .
}

How do I get rid of that exception?


Solution

  • Reason:

    2022-03-08T20:53:02.990 will map to LocalDateTime.

    2022-03-08T20:53:02.990Z will map to Instant.

    Fix: