I think I've read every past post on this issue and it still isn't working for me. My setup:
OpenJDK 64-Bit 11.0.14.1+1 Jackson module version 2.14.1
I have the following Jackson modules included:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
</dependency>
My mapper is instantiated as such having all the relevant Java dependencies above:
ObjectMapper jsonObjectMapper = new ObjectMapper()
.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule());
And my Java objects have various LocalDateTime properties defined as follows with the relevant annotations:
@JsonProperty("created")
@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd")
@JsonDeserialize(using= LocalDateTimeDeserializer.class) //Tried with and without this line
private LocalDateTime created;
But when it parses I get the error:
Cannot deserialize value of type `java.time.LocalDateTime` from String "2021-10-15":
Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException)
Text '2021-10-15' could not be parsed: Unable to obtain LocalDateTime from
TemporalAccessor: {},ISO resolved to 2021-10-15 of type java.time.format.Parsed
So it seems to not be using the custom formatter. I then tried adding DateTimeFormatter directly using the following:
JavaTimeModule jtm = new JavaTimeModule();
jtm.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
ObjectMapper mapper = new ObjectMapper()
.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(jtm);
But that didn't help. What am I missing?
Since
java.timeAPI fails to constructLocalDateTimeobjects from strings with missing time part (there are obvious defaults though), you have following options:LocalDate->LocalDateTimeconversion and vice versa:@JsonDeserialize#converterfeature ofjackson:LocalDateinstead ofLocalDateTime