0

I need to transform a java object versementDTO to json string Historique, this DTO contains some Dates, jackson is transforming dates to an object like that : "dateValidation":{"nano":0,"year":2007,"monthValue":2,"dayOfMonth":7,"hour":15,"minute":21,"second":24,"month":"FEBRUARY","dayOfWeek":"WEDNESDAY","dayOfYear":38,"chronology":{"id":"ISO","calendarType":"iso8601"}}, and I need to get a value like : "2007/02/21 15:21:24" and I get the following error : Resolved

[org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "2007-02-07T15:21:24": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2007-02-07T15:21:24' could not be parsed at index 10; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2007-02-07T15:21:24": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2007-02-07T15:21:24' could not be parsed at index 10
 at [Source: (PushbackInputStream); line: 1, column: 95] (through reference chain: aws.lbackend.dto.VersementDTO["dateValidation"])] 

appriciating your help !

public static String historizeInJson(VersementDTO pojo) {

    SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm");
    df.setTimeZone(TimeZone.getTimeZone("UTC"));

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true));

    objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
    objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
    try {
        String jsVDTO = objectMapper.writeValueAsString(pojo);
        //System.out.print("json dz : "+ jsVDTO);
        return jsVDTO;
    } catch (JsonProcessingException e) {
            LOGGER.info("failed conversion: Pfra object to Json", e);
        return null;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Jens
  • 67,715
  • 15
  • 98
  • 113
Organon
  • 23
  • 4
  • Do not longer use outdated classes `SimpleDateFormat`. Switch to `java.time.DateTimeFormatter` – Jens Jan 13 '23 at 10:48
  • Does this answer your question? [Deserialize Java 8 LocalDateTime with JacksonMapper](https://stackoverflow.com/questions/40327970/deserialize-java-8-localdatetime-with-jacksonmapper) – Jens Jan 13 '23 at 10:48
  • It didn't work :/ I had to serialize LocalDateTime like this : – Organon Jan 17 '23 at 12:46

1 Answers1

0
ObjectMapper objectMapper = new ObjectMapper();

SimpleModule module = new SimpleModule("ModuleDate2Json",
                new Version(1, 0, 0, null));
module.addSerializer(LocalDateTime.class, new LocalDateSerializer<LocalDateTime>());
        objectMapper.registerModule(module);

with LocalDateSerialzer class:

public class LocalDateSerializer<L> extends JsonSerializer<LocalDateTime {

    public LocalDateSerializer() {
        super();
    }

    @Override
    public void serialize(LocalDateTime localDateTime, org.codehaus.jackson.JsonGenerator jsonGenerator, org.codehaus.jackson.map.SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        jsonGenerator.writeString(localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    }
}
Alex
  • 4,987
  • 1
  • 8
  • 26
Organon
  • 23
  • 4