0
votes

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')

1
What does this have to do with IBM MQ? - JoshMc
no that not answer - robert trudel
can you try with @JsonDeserialize(using = LocalDateDeserializer.class) @JsonSerialize(using = LocalDateSerializer.class) on billingCreatedDate attribute. - dassum
This would mean that the module to convert to java.time classes (JavaTimeModule) hasn't been registered with the object mapper. - Mark Rotteveel

1 Answers

0
votes

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;