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?

3
  • Did you check this question: stackoverflow.com/questions/28802544/…? Commented Nov 4, 2022 at 11:36
  • I just implemented this solution but its still not working Commented Nov 4, 2022 at 14:27
  • Check for ObjectMapper with DateTime option, i don't remember exactly but it is related to that, i faced this long back Commented Nov 4, 2022 at 16:25

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

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
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
If my solution helps then request you to please accept the answer so that it help others as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.