2016-04-30 74 views
0

我有這樣一個類:無法從字符串值實例類型[簡單類型,類java.time.LocalDate]的價值

@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:

{ 
    "_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: Can not instantiate value of type [simple type, class java.time.LocalDate] from String value ('2016-10-10');

我相信jakson和普及工作正常。

+0

@peeskillet ... –

+0

也許這個答案將幫助您:http://stackoverflow.com/questions/28802544/java-8-localdate-jackson-format – rapasoft

回答

1

老問題,但我最近不得不爲自己回答。 有不同的解決方案(如rapasoft評論,見例如here)。 我使用的快速解決方案涉及添加用於反序列化的setDate(String)方法。 它可能不是最漂亮的解決方案,但它不會更新其他類。 下面一個可運行的類來說明:

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(); 
     } 
    } 
} 
相關問題