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

vi4fp9gy  于 12个月前  发布在  Java
关注(0)|答案(3)|浏览(151)
{
"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"
    }
  ]
 
}

qeeaahzv

qeeaahzv1#

添加日期属性

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)

字符串

ybzsozfc

ybzsozfc2#

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
前面的答案对我来说很好。只有添加,如果你需要更改日期格式,你可以使用这句话:
第:值="${#temporals.format(fromtemporalDate,'yyyy-MM-dd')}”

8zzbczxx

8zzbczxx3#

您可以在模型类中使用此方法,而不是@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注解应该有助于获得具有关键名称的数据。private LocalDate toDepartureDate行正在为您的模型类定义变量类型。如果输入了正确的数据,它将被正确分配。并且还具有串行化和去串行化,如下所示,它对我有效。

相关问题