java - 无法将类型 [java.lang.String] 的属性值转换为所需类型 [java.time.LocalDate]

标签 java json api spring-boot jackson

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

} 

最佳答案

添加您的日期属性

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)

关于java - 无法将类型 [java.lang.String] 的属性值转换为所需类型 [java.time.LocalDate],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41366031/

相关文章:

php - 使用 salesforce api 将联系人与帐户相关联

java - 如何在Java中遍历表数据并将其传递给servlet以便在JSP中使用

javascript - 通过调用带参数的 URL 获取 json 对象

javascript - 当我尝试运行 mt Node.js 应用程序时出现 TypeError

android - 如何使用 Retrofit 发布复杂的 JSON 参数

android - 如何使用 Retrofit 2 从 JSON 列表中获取 List<String>?

mysql - 如何使用 Node Js 创建嵌套 JSON 响应

java - ComboBoxCellEditor 不更改标签

java - Spring:具有两个参数 setter 的配置 Bean

java - Spring方面调用接口(interface)方法上的自定义注释