Jsonformat with Localdatetime springboot

Asked

Viewed 283 times

0

Hello, I am trying to record in the bank only the time of the attribute of my Entity, I put @Jsonformat(Pattern = "HH:mm:ss") but the error when it will record

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "19:47:11": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '19:47:11' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 19:47:11 of type java.time.format.Parsed; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "19:47:11": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '19:47:11' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 19:47:11 of type java.time.format.Parsedat [Source: (PushbackInputStream); line: 10, column: 20] (through reference chain: br.com.lucas.entity.Acao["entrada"])]

searching to see this error put the annotation @Jsondeserialize, but without success but if I leave thus @Jsonformat(Pattern = "dd-MM-yyyy HH:mm:ss") it records, logical by passing the date together but wanted only the hour:minute:second. Someone’s been through this trouble and could talk like?

1 answer

0


First thing, if you only need hour, minute and second, use Localtime and not Localdatetime, as per the documentation https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html

2 - I am using Lombok in this example code and some concepts of immutability, you can deserialize the date as follows

Deserializer

public class DateDeserializer{
    private DateDeserializer() {
        super();
    }

    public static LocalTime localTime(String date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        return LocalDate.parse(date, formatter);
    }
}
@Value
@With
@JsonDeserialize(builder = Acao.JacksonBuilder.class)
@Builder(builderClassName = "JacksonBuilder")
public class Acao {

   LocalTime entrada;


@JsonPOJOBuilder(withPrefix = "")
    public static class JacksonBuilder {

       LocalTime entrada;

       public JacksonBuilder dataVencimentoReal(String entrada) {
            this.entrada = DateDeserializer.localTime(entrada);
            return this;
        }
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.