serialize/deserialize java 8 java.time with Jackson JSON mapper

后端 未结 17 1355
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 15:56

How do I use Jackson JSON mapper with Java 8 LocalDateTime?

org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple t

17条回答
  •  被撕碎了的回忆
    2020-11-22 16:42

    If you're having this issue because of GraphQL Java Tools and trying to marshal an Java Instant from a date string, you need to setup your SchemaParser to use an ObjectMapper with certain configurations:

    In your GraphQLSchemaBuilder class, inject ObjectMapper and add this modules:

            ObjectMapper objectMapper = 
        new ObjectMapper().registerModule(new JavaTimeModule())
                .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    

    and add it to the options:

    final SchemaParserOptions options = SchemaParserOptions.newOptions()
                .objectMapperProvider(fieldDefinition -> objectMapper)
                .typeDefinitionFactory(new YourTypeDefinitionFactory())
                .build();
    

    See https://github.com/graphql-java-kickstart/graphql-spring-boot/issues/32

提交回复
热议问题