1

I have a JSON String and I am using Jackson to parse it into a POJO. The incoming JSON string has a different format, so I had to write my own converter to convert the String to LocalTimeDate object. I use 2 annotations to achieve this.

public class Content {

    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @TDateFormat
    private LocalDateTime modifiedDate;
}

TDateFormat: This annotation specifies the date format we need. It also has a default value.

@Retention(RetentionPolicy.RUNTIME)
public @interface TDateFormat {
    String value() default "MMM d, yyyy, hh:mm:ss a";
}

LocalDateTimeDeserializer: This holds the logic to deserialize. We use the TDateFormat value get the format to use. I have extracted the logic out to an Util class

@NoArgsConstructor
public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> implements ContextualDeserializer {

    private String format;

    public LocalDateTimeDeserializer(String format) {
        this.format = format;
    }
    @Override
    public JsonDeserializer<?> createContextual(DeserializationContext dc, BeanProperty bp) {
        return new LocalDateTimeDeserializer(bp.getAnnotation(TDateFormat.class).value());
    }

    @Override
    public LocalDateTime deserialize(JsonParser jp, DeserializationContext dc) throws IOException {
        LocalDateTime localDateTime = Utils.convertDate(jp.getText(), format);
        System.out.println("Done::: " + localDateTime);
        return localDateTime;
    }
}

The above code works fine. Now, I am trying to combine @TDateFormat and @JsonDeserialize(using = LocalDateTimeDeserializer.class) to @TDeserializer

@TDeserializer Looks like below.

@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@TDateFormat
public @interface TDeserializer {
}

And I want to use the annotation like this

public class Content {

    @TDeserializer
    private LocalDateTime modifiedDate;
}

But I get the following error, when I make this little change.

2022-09-29 23:32:09.569 ERROR 90506 --- [ntainer#0-0-C-1] c.t.k.service.impl.KafkaMessageConsumer  : Exception occurred::: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling
 at [Source: (String)"{"modifiedDate":"Sep 29, 2022, 11:25:56 PM" [truncated 149 chars]; line: 1, column: 52] (through reference chain: com.techopact.kafkautil.model.Article["modifiedDate"])

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling

Can anyone please let me know what more I have to do to combine the annotations?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Andy
  • 5,433
  • 6
  • 31
  • 38
  • Add `com.fasterxml.jackson.datatype:jackson-datatype-jsr310` module to your `ObjectMapper`. Add dependency and register it manually if it is not loaded automatically - `objectMapper.registerModule(new JavaTimeModule());` See that question: [Could not find artifact Jackson-modules-java8:jar](https://stackoverflow.com/questions/54810554/could-not-find-artifact-jackson-modules-java8jar/54810984#54810984) – Michał Ziober Oct 03 '22 at 13:55
  • @MichałZiober, Will try that. I am wonder why we should add a dependency just to combine the annotations. That's a Java thing, right? – Andy Oct 05 '22 at 15:21
  • 1
    `jackson-datatype-jsr310` module implements serializing and deserializing of `Java 8` date types. This is why you need to include this module. – Michał Ziober Oct 05 '22 at 15:36

0 Answers0