0

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?

Rômulo Sorato
  • 1,570
  • 5
  • 18
  • 29

1 Answers1

1

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;
}
Rohit Agarwal
  • 763
  • 2
  • 5
  • 10
  • Removing the @Builder annotation returns me the following error: "message": "JSON parse error: Cannot construct instance of `com.launchersoft.vouchersapi.dto.OfertaEspecialDTO` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.launchersoft.vouchersapi.dto.OfertaEspecialDTO` (although at least one Creator exists): cannot deserialize from Object value (no – Rômulo Sorato Nov 04 '22 at 16:51
  • Yes you need to remove final keyword from desconto_percentual variable. Then this error will be gone. Please check my solution I am not using final keyword – Rohit Agarwal Nov 04 '22 at 17:23
  • 1
    If my solution helps then request you to please accept the answer so that it help others as well. – Rohit Agarwal Nov 06 '22 at 13:16