0

In a spring boot 2.2.2, java 11 a object is received via Ibm mq.

Object received have LocalDate data type.

Project have spring-boot-starter-web starter in maven.

I see theses jar in the project

jackson datatype -jdk 8-2.10.1 jackson-datatype-jsr310-2.10.1

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class BillingEvent {
    public Long Id;
    public LocalDate billingCreatedDate;
}

In my properties I have

spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false

Error I get

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of java.time.LocalDate (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2019-09-02')

pirho
  • 11,565
  • 12
  • 43
  • 70
robert trudel
  • 5,283
  • 17
  • 72
  • 124
  • What does this have to do with IBM MQ? – JoshMc Dec 20 '19 at 02:03
  • Does this answer your question? [Problem with deserialization of LocalDateTime in Junit test](https://stackoverflow.com/questions/55107588/problem-with-deserialization-of-localdatetime-in-junit-test), [Jackson Serialize Instant to Nanosecond Issue](https://stackoverflow.com/questions/56345200/jackson-serialize-instant-to-nanosecond-issue), [Jackson deserialize elasticsearch long as LocalDateTime with Java 8](https://stackoverflow.com/questions/57098784/jackson-deserialize-elasticsearch-long-as-localdatetime-with-java-8) – Michał Ziober Dec 20 '19 at 10:25
  • no that not answer – robert trudel Dec 20 '19 at 12:17
  • 1
    can you try with @JsonDeserialize(using = LocalDateDeserializer.class) @JsonSerialize(using = LocalDateSerializer.class) on billingCreatedDate attribute. – dassum Dec 20 '19 at 13:23
  • This would mean that the module to convert to `java.time` classes (`JavaTimeModule`) hasn't been registered with the object mapper. – Mark Rotteveel Dec 20 '19 at 16:48

1 Answers1

0

For me it was enough to add setter to your BillingEvent, like:

public void setBillingCreatedDate(String str) {
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");        
    billingCreatedDate = LocalDate.parse(str, formatter);
}

More about formatting here: String to LocalDate

Based on comments:

this Is there a jackson datatype module for JDK8 java.time? might help you and if not it is not for help, below an example of working impl. (without any checks or so):

public class LocalDateDeserializer extends JsonDeserializer<LocalDate> {

    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @Override
    public LocalDate deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {        
          return LocalDate.parse(p.readValueAs(String.class), formatter);
    }

}

you could use it also elsewhere like you would use it in your BillingEvent:

@JsonDeserialize(using = LocalDateDeserializer.class)
public LocalDate billingCreatedDate;
pirho
  • 11,565
  • 12
  • 43
  • 70