【问题标题】:Failed to convert property value of type [java.lang.String] to required type [java.time.LocalDate]无法将类型 [java.lang.String] 的属性值转换为所需类型 [java.time.LocalDate]
【发布时间】:2017-05-12 23:19:26
【问题描述】:
{
"toDepartureDate": "2016-12-28",
"fromDepartureDate": "2016-12-28"
}

我想在 json 中以 java.time.LocalDate 的形式发布上述字符串日期,但我收到 400 Bad Request 作为错误。有人可以在这里帮忙吗?我用过@JsonFormat,但它也没有帮助我。

@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"
    }
  ]

} 

【问题讨论】:

标签: java json api spring-boot jackson


【解决方案1】:

添加日期属性

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)

【讨论】:

    【解决方案2】:

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)

    上一个答案对我来说很好。只是补充一下,如果需要更改日期格式,可以使用这句话:

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

    【讨论】:

      猜你喜欢
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 2018-08-21
      • 2017-04-21
      • 2021-01-23
      • 2018-10-13
      • 2017-06-14
      相关资源
      最近更新 更多