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);
}
}