无法从String值实例化类型[simple type,class java.time.LocalDate]的值

时间:2016-04-30 09:24:59

标签: java spring

我有一个这样的课程:

@Data
@NoArgsConstructor(force = true)
@AllArgsConstructor(staticName = "of")
public class BusinessPeriodDTO {
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    LocalDate startDate;
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    LocalDate endDate;
}

我在另一个班级中使用了这个班级,让我们称之为PurchaseOrder

@Entity
@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED, force = true)
public class PurchaseOrder {
    @EmbeddedId
    PurchaseOrderID id;

    @Embedded
    BusinessPeriod rentalPeriod;

    public static PurchaseOrder of(PurchaseOrderID id, BusinessPeriod period) {
        PurchaseOrder po = new PurchaseOrder();
        po.id = id;

        po.rentalPeriod = period;

        return po;
    }

我试图使用jakson和这个JSON填充purchaseOrder记录:

 {
     "_class": "com.rentit.sales.domain.model.PurchaseOrder",
     "id": 1,
     "rentalPeriod": {
         "startDate": "2016-10-10",
         "endDate": "2016-12-12"
     }
 }

但我遇到了一个错误:

  

java.lang.RuntimeException:com.fasterxml.jackson.databind.JsonMappingException:无法从String值实例化类型[simple type,class java.time.LocalDate]的值(' 2016-10-10&# 39);

我确信jakson和popularization正常工作。

2 个答案:

答案 0 :(得分:3)

包含在您的pom.xml中:

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-jsr310</artifactId>
  <version>2.9.6</version>
</dependency>

然后在您的BusinessPeriodDTO.java导入LocalDateDeserializer中,如下所示:

import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;

最后,总是在您的BusinessPeriodDTO.java文件中,对感兴趣的日期进行如下注释:

@JsonDeserialize(using = LocalDateDeserializer.class)
LocalDate startDate;
@JsonDeserialize(using = LocalDateDeserializer.class)
LocalDate endDate;

答案 1 :(得分:1)

老问题,但我最近不得不自己回答。 有不同的解决方案(由rapasoft评论,例如参见here)。 我使用的快速解决方案涉及添加setDate(String)方法进行反序列化。 它可能不是最漂亮的解决方案,但它可以在不更新其他类的情况下工作。 在runnable类下面演示:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

/**
 * Demonstrate Java 8 date/time (de)serialization for JSON with Jackson databind.
 * Requires {@code com.fasterxml.jackson.core:jackson-databind:2.8.5} 
 * and {@code com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.8.5} 
 */
public class JdateDto {

    /** The pattern as specified by {@link java.text.SimpleDateFormat} */
    public static final String ISO_LOCAL_DATE_PATTERN = "yyyy-MM-dd";

    /* Used when serializing isoLocalDate. */
    @JsonFormat(shape = Shape.STRING, pattern = ISO_LOCAL_DATE_PATTERN)
    private LocalDate isoLocalDate;

    public LocalDate getIsoLocalDate() {
        return isoLocalDate;
    }

    /* Used when deserializing isoLocalDate. */
    public void setIsoLocalDate(String date) {
        setIsoLocalDate(LocalDate.parse(date, DateTimeFormatter.ISO_LOCAL_DATE));
    }

    public void setIsoLocalDate(LocalDate isoDate) {
        this.isoLocalDate = isoDate;
    }

    public static void main(String[] args) {

        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.registerModule(new JavaTimeModule());
            JdateDto dto = new JdateDto();
            dto.setIsoLocalDate(LocalDate.now());
            String json = mapper.writeValueAsString(dto);
            System.out.println(json);
            JdateDto dto2 = mapper.readValue(json, JdateDto.class);
            if (dto.getIsoLocalDate().equals(dto2.getIsoLocalDate())) {
                System.out.println("Dates match.");
            } else {
                System.out.println("Dates do not match!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}