2017-01-07 179 views
2

這裏我使用java 1.8的LocalDate類。在我的bean中,我將返回類型作爲LocalDate。我發送的日期是07/01/2017。當我試圖保存,我得到follwing異常。不能實例化類型的值[簡單類型,類java.time.LocalDate

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-33) Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not instantiate value of type [simple type, class java.time.LocalDate] from String value ('07/01/2017'); no single-String constructor/factory method 
at [Source: [email protected]; line: 1, column: 650] (through reference chain: com.pro.bean.ParentBean["Soici"]->com.pro.bean.Soici["fecha_de_solicitud"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDate] from String value ('07/01/2017'); no single-String constructor/factory method 
at [Source: [email protected]; line: 1, column: 650] (through reference chain: com.pro.bean.ParentBean["Soici"]->com.pro.bean.Soici["fecha_de_solici"]) 
+0

我看來春天的PropertyEditor問題的問題。看看,你會發現有用的信息在線 –

+1

看看這個答案這將有助於:http://stackoverflow.com/a/14052884/4828463這個答案使用@InitBinder,它允許你設置你自己的編輯器,而不是propertyeditor –

回答

3

添加com.fasterxml.jackson.datatype-jsr310依賴

然後用

@Bean(name = "OBJECT_MAPPER_BEAN") 
    public ObjectMapper jsonObjectMapper() { 
     return Jackson2ObjectMapperBuilder.json() 
       .serializationInclusion(JsonInclude.Include.NON_NULL) // Don’t include null values 
       .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) //ISODate 
       .modules(new JSR310Module()) 
       .build(); 
    } 

源配置Jackson2ObjectMapper:https://blog.oio.de/2015/06/13/add-support-for-java-8-date-time-api-to-jackson-serialized-rest-web-services/

+0

取決於關於他如何配置(特別是Boot),他可能只需要添加依賴關係。 – chrylis

+0

爲什麼我們只需要遵循這麼多的東西?本地日期 –

+1

@Ravi:'java.time'庫是在Java 8中引入的。推測它們與它們的兼容性保留在依賴關係中,以使得使用舊版本的用戶不能包括他們。 – Henrik

相關問題