Home > front end >  JSON parse error: Cannot deserialize value of type `java.time.LocalDate`
JSON parse error: Cannot deserialize value of type `java.time.LocalDate`

Time:11-07

Im trying to send a post to my api using postman:

enter image description here

But its returning an error:

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type java.time.LocalDate from String "10/11/2022": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '10/11/2022' could not be parsed at index 0;

I tried to correct do the mapping with json mapping annotation in the dto class:

@Data
@Builder
public class OfertaEspecialDTO {

    private String nome;
    private final double desconto_percentual;
    private String descricao;
    @JsonFormat(pattern = "dd/MM/yyyy", shape = JsonFormat.Shape.STRING)
    private LocalDate dataValidade;
}

But its still returning me the error.

How is the correct way to map my dateTime instance variable?

CodePudding user response:

There is no issue with LocalDate instance variable mapping. Issue is with annotations used on top of class. Please refactor DTO class like this and try again.

@Setter
@Getter
public class OfertaEspecialDTO {
    private String nome;
    private double desconto_percentual;
    private String descricao;
    @JsonFormat(pattern = "dd/MM/yyyy", shape = JsonFormat.Shape.STRING)
    private LocalDate dataValidade;
}

or like this

@Data
public class OfertaEspecialDTO {
    private String nome;
    private double desconto_percentual;
    private String descricao;
    @JsonFormat(pattern = "dd/MM/yyyy", shape = JsonFormat.Shape.STRING)
    private LocalDate dataValidade;
}
  • Related