我正在使用Angular日期選擇器向我的MVC控制器發送日期,使用ISO Date日期對象/ 時間。傑克遜從ISO反序列化Joda LocalDate DateTime
關於反序列化java.util.Date
它的作用就像一個魅力,Hibernate會關心在插入記錄時將那個時間切割成普通日期。
但現在我正在從java.util.Date
轉換到org.joda.time.[APPROPRIATE_CLASS_HERE]
我正面臨着這個反序列化問題。
我的理解是,如果我在我的DTO中強制DateTime
,Jackson會正確反序列化它們,而我更傾向於在目標類型爲Date時刪除時間信息。
E.g.
public class UserDto {
private LocaLDate passwordExpirationDate;
}
{
"username":"9493",
"completeName":"ljdjf",
"email":"[email protected]",
"cultureId":"IT",
"enabled":false,
"passwordExpirationDate":"2017-07-13T10:00:00.000Z",
"accountExpirationDate":"2017-07-20T10:00:00.000Z"
}
相反,我得到這個:
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Invalid format: "2017-07-13T10:00:00.000Z" is malformed at "T10:00:00.000Z"; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Invalid format: "2017-07-13T10:00:00.000Z" is malformed at "T10:00:00.000Z" (through reference chain: it.phoenix.web.data.dtos.admin.profile.UserDTO["passwordExpirationDate"])
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:244) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Invalid format: "2017-07-13T10:00:00.000Z" is malformed at "T10:00:00.000Z" (through reference chain: it.phoenix.web.data.dtos.admin.profile.UserDTO["passwordExpirationDate"])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:388) ~[jackson-databind-2.8.9.jar:2.8.9]
Caused by: java.lang.IllegalArgumentException: Invalid format: "2017-07-13T10:00:00.000Z" is malformed at "T10:00:00.000Z"
at org.joda.time.format.DateTimeFormatter.parseLocalDateTime(DateTimeFormatter.java:900) ~[joda-time-2.9.9.jar:2.9.9]
at org.joda.time.format.DateTimeFormatter.parseLocalDate(DateTimeFormatter.java:844) ~[joda-time-2.9.9.jar:2.9.9]
at com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer.deserialize(LocalDateDeserializer.java:39) ~[jackson-datatype-joda-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer.deserialize(LocalDateDeserializer.java:15) ~[jackson-datatype-joda-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:504) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:104) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:357) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:148) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3814) ~[jackson-databind-2.8.9.jar:2.8.9]
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2938) ~[jackson-databind-2.8.9.jar:2.8.9]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:241) ~[spring-web-4.3.9.RELEASE.jar:4.3.9.RELEASE]
... 92 more
問題是:有一個聰明的辦法讓傑克遜可以通過簡單的剝離,在默認情況下,部分時間/當前時區DateTime對象解碼成喬達LocalDate
?
注: - 我已經有傑克遜喬達模塊依賴 - 傑克遜是2.8.9 - 我不得不使用Java 7。在相關的Java 8項目我沒有這樣的問題java.time
東西(和傑克遜JSR310模塊)
任何機會,在全球範圍內應用此之後... ... EHM類路徑範圍內? –
另外...我剛完成測試提議的解決方案。我會發布它作爲答案。如果您可以幫助我將上述內容應用於LocalDate類型的每個屬性,我將授予您接受的答案,因爲我不喜歡我的解決方案 –
如何配置傑克遜?你是手動創建ObjectMapper還是使用Angular特定的配置文件來配置它? – 2017-07-19 17:21:19