如何将 graphql-java-extended-scalars DateTime 与 Jackson 一起使用
Posted
技术标签:
【中文标题】如何将 graphql-java-extended-scalars DateTime 与 Jackson 一起使用【英文标题】:How to use graphql-java-extended-scalars DateTime with Jackson 【发布时间】:2019-12-05 07:14:24 【问题描述】:我收到了这个错误
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);
我正在发送
...,
start: "2019-07-27T01:32:33.116Z",
end: "2019-07-27T11:32:33.116Z"
基本上是
...,
start: moment().format(),
end: moment().format()
架构
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
【问题讨论】:
【参考方案1】:这个问题实际上与 GraphQL 无关......它是关于配置 Jackson 以反序列化 JodaTime 类型。尝试正确地隔离您的问题,因为这样可以消除很多干扰,让问题更容易回答,也更容易被更广泛的受众所接受。
无论如何,您正在注册JavaTimeModule
,但您使用的是 JodaTime。你需要JodaModule
。
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.9.5</version>
</dependency>
然后
mapper.registerModule(new JodaModule());
【讨论】:
这是我的类似错误:***.com/questions/67746066/…我该如何解决?以上是关于如何将 graphql-java-extended-scalars DateTime 与 Jackson 一起使用的主要内容,如果未能解决你的问题,请参考以下文章