0

I have an issue with mapping a JSON to my DTO that has a LocalDateTime.

I've followed this thread: JSON parse error: Can not construct instance of java.time.LocalDate: no String-argument constructor/factory method to deserialize from String value

Added to build.gradle

implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.0'

Here is my variable in my DTO:

@Data
public class MyDto {
    private Long teamId;

    private Map<String, List<Long>> details;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime dateOccurred;

}

And added this

public class MyApplication {
    @Autowired
    private ObjectMapper objectMapper;

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @PostConstruct
    public void setUp() {
        objectMapper.registerModule(new JavaTimeModule());
    }
}

Did I miss something? I'm getting this error

org.springframework.messaging.converter.MessageConversionException: Could not read JSON: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2020-06-16 11:12:46')

Thank you!

mengmeng
  • 1,266
  • 2
  • 22
  • 46

1 Answers1

2

Thank you to @mohammedkhan for the guide!

In this answer: JSON Java 8 LocalDateTime format in Spring Boot

There's already a setting in spring-boot there's no need to @Autowire the ObjectMapper

dd this annotation for the Json Deserializer

@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime dateOccurred;

Thank you!

mengmeng
  • 1,266
  • 2
  • 22
  • 46
  • A _serialization_ property won't help in the **deserialization** that you're doing. Also `LocalDateTimeDeserializer` is already responsible for deserializing `LocalDateTime`, assuming you correctly registered the `JavaTimeModule` (you can see that in its constructor implementation). It's not needed here (unless there's something in your code you're not showing us). – Sotirios Delimanolis Jun 25 '20 at 15:15
  • @SotiriosDelimanolis hi sir. i'm not sure if there's something else. Those were the only changes that I've made and it's working rn. This is the link attached to the answer that I was checking. http://blog.chris-ritchie.com/2014/09/java-8-localdate-with-jackson-serialize.html – mengmeng Jun 25 '20 at 15:27
  • Let me know if you have any other insights :) – mengmeng Jun 25 '20 at 15:27
  • That blog post from 2014 shows what you would do if you're not registering `JavaTimeModule`. According to the code in your question, you are, so it's not needed. – Sotirios Delimanolis Jun 25 '20 at 15:29
  • @SotiriosDelimanolis https://stackoverflow.com/questions/51527794/how-do-i-configure-jackson-serialization-on-localdatetime-and-localdate-for-java/51546069 I've tried the another to these. The two method but it still doesn't work :( – mengmeng Jun 25 '20 at 15:56
  • Only by inclusion of the jsr310 and the above annotations worked for me. Also no need of the config in app yaml ```spring: jackson: serialization: write-dates-as-timestamps: false ``` – Kartik Narayana Maringanti May 10 '23 at 18:44