Chandan Gawri
Chandan Gawri

Reputation: 431

Failed to convert property value of type [java.lang.String] to required type [java.time.LocalDate]

{
"toDepartureDate": "2016-12-28",
"fromDepartureDate": "2016-12-28"
}

I want to post above String dates in json as java.time.LocalDate, but I am receiving 400 Bad Request as error. Could some one help here. I have used @JsonFormat but it did not help me either.

@JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd",timezone = "GMT+5:30")

private LocalDate fromDepartureDate;

@JsonFormat(shape=JsonFormat.Shape.STRING,pattern="yyyy-MM-dd",timezone = "GMT+5:30")
private LocalDate toDepartureDate;

{
  "timestamp": 1482942147246,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.validation.BindException",
  "errors": [
    {
      "codes": [
        "typeMismatch.flightReportSearchDto.fromDepartureDate",
        "typeMismatch.fromDepartureDate",
        "typeMismatch.java.time.LocalDate",
        "typeMismatch"
      ],
      "arguments": [
        {
          "codes": [
            "flightReportSearchDto.fromDepartureDate",
            "fromDepartureDate"
          ],
          "arguments": null,
          "defaultMessage": "fromDepartureDate",
          "code": "fromDepartureDate"
        }
      ],
      "defaultMessage": "Failed to convert property value of type [java.lang.String] to required type [java.time.LocalDate] for property 'fromDepartureDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat java.time.LocalDate] for value '2016-12-28'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2016-12-28]",
      "objectName": "flightReportSearchDto",
      "field": "fromDepartureDate",
      "rejectedValue": "2016-12-28",
      "bindingFailure": true,
      "code": "typeMismatch"
    },
    {
      "codes": [
        "typeMismatch.flightReportSearchDto.toDepartureDate",
        "typeMismatch.toDepartureDate",
        "typeMismatch.java.time.LocalDate",
        "typeMismatch"
      ],
      "arguments": [
        {
          "codes": [
            "flightReportSearchDto.toDepartureDate",
            "toDepartureDate"
          ],
          "arguments": null,
          "defaultMessage": "toDepartureDate",
          "code": "toDepartureDate"
        }
      ],
      "defaultMessage": "Failed to convert property value of type [java.lang.String] to required type [java.time.LocalDate] for property 'toDepartureDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat java.time.LocalDate] for value '2016-12-29'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2016-12-29]",
      "objectName": "flightReportSearchDto",
      "field": "toDepartureDate",
      "rejectedValue": "2016-12-29",
      "bindingFailure": true,
      "code": "typeMismatch"
    }
  ]
 
} 

Upvotes: 3

Views: 13385

Answers (3)

Nakip Ali
Nakip Ali

Reputation: 40

You can use this method in your model class instead of @JsonFormat

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(Include.NON_NULL)
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class YourModelClass implements Serializable {
    private static final long serialVersionUID = -4399291572854602191L;
    ...
    ...
    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    @JsonProperty("toDepartureDate")
    private LocalDate toDepartureDate;
    ....
    ....
  }

@JsonProperty annotation should be help to get data with key name. private LocalDate toDepartureDate line is defining your variables type for your model class. If the correct data entered , it will be assigned correctly. And also with serilization and deserilization like as below , it worked for me .

Upvotes: 0

javivas
javivas

Reputation: 1

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)

The previous answer works fine for me. Only add, that if you need to change date format, you can use this sentence:

th:value="${#temporals.format(fromDepartureDate,'yyyy-MM-dd')}"

Upvotes: 0

dtelaroli
dtelaroli

Reputation: 1275

Add in your date property

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)

Upvotes: 6

Related Questions