简体   繁体   中英

How to use graphql-java-extended-scalars DateTime with Jackson

I am getting this error

Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `org.joda.time.DateTime` (although at least one Creator exists): no suitable creator method found to deserialize from Number value (1564191690.702000000)
 at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: br.com.b2breservas.api.dto.FakeReservationPaginationInput["filter"]->br.com.b2breservas.api.dto.FakeReservationPaginationInputFilter["end"])

Java

import org.joda.time.DateTime;

public class FakeReservationPaginationInput {

    FakeReservationPaginationInputFilter filter;

    CheckoutPermissionInput permission;


}

public class FakeReservationPaginationInputFilter {
    Integer limit;
    List<String> statuses;
    String query;
    DateTime start;
    DateTime end;
}

private final ObjectMapper objectMapper = new ObjectMapper()
        .registerModule(new ParameterNamesModule())
        .registerModule(new Jdk8Module())
        .registerModule(new JavaTimeModule());

// where the error pops up
FakeReservationPaginationInput input = objectMapper
                .convertValue(dataFetchingEnvironment.getArgument("input"), FakeReservationPaginationInput.class);

I am sending

{
     ..., 
     start: "2019-07-27T01:32:33.116Z",
     end: "2019-07-27T11:32:33.116Z"
}

which is basically

{
     ..., 
     start: moment().format(),
     end: moment().format()
}

ž

Schema

scalar DateTime

input QueryReservationsInput {
    filter: ReservationFilter
    permission: CheckoutPermissionInput
}

input ReservationFilter {
    query: String
    statuses: [String]
    limit: Int
    end: DateTime!
    start: DateTime!
}

input CheckoutPermissionInput {
    type: String
    ref_id: Int
    ref_name: String
    chain_id: Int
    chain_name: String
}

This question has nothing to do with GraphQL really... It's about configuring Jackson to deserialize JodaTime types. Try to isolate your problem properly as that removes a lot of noise and makes the question easier to answer and accessible to a wider audience.

Anyway, you're registering JavaTimeModule yet you use JodaTime. You need JodaModule instead.

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-joda</artifactId>
  <version>2.9.5</version>
</dependency>

And then

mapper.registerModule(new JodaModule());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM