本文介绍了杰克逊将从ISO DateTime反序列化Joda LocalDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Angular日期选择器通过Javascript Date对象(它是ISO日期/ 时间)向MVC控制器发送日期。

I am using Angular date picker to send my MVC controller a date, using a Javascript Date object which is an ISO date/time.

在反序列化 java.util.Date 时,它像一种魅力,Hibernate会在意将 Date 的时间减少到

On deserializing java.util.Date it works like a charm and Hibernate will care about cutting that Datetime to a plain Date when inserting records.

但是现在我正在从 java.util.Date 转换为 org.joda.time。[APPROPRIATE_CLASS_HERE] 我正面临着反序列化问题。

But now that I am transitioning from java.util.Date to org.joda.time.[APPROPRIATE_CLASS_HERE] I am facing this deserialization issue.

据我了解,如果我强制执行我的DTO中的 DateTime 杰克逊会正确地反序列化它们,而我更喜欢在目标类型为日期时删除时间信息。

It is my understanding that if I force DateTime in my DTOs Jackson will deserialize them correctly, while instead I prefer to drop time information when the target type is a Date.

例如

public class UserDto {

    private LocaLDate passwordExpirationDate;

}


{
   "username":"9493",
   "completeName":"ljdjf",
   "email":"wesf@dsgfds",
   "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对象解码为Joda LocalDate

Question is: is there a smart way so that Jackson can decode DateTime object into a Joda LocalDate by simply stripping the time part at default/current time zone?

注意:
-我已经具有Jackson Joda模块依赖性
-Jackson是2.8.9
-我被迫使用Java7。在一个相关的Java 8项目中,我拥有没有这样的问题 java.time 东西(和Jackson JSR310模块)

Notes: - I already have Jackson Joda module dependency - Jackson is 2.8.9 - I am forced to use Java 7. In a related Java 8 project I have no such problem with java.time stuff (and Jackson JSR310 module)

推荐答案

以下在我的Spring上下文配置中对我有用

The following worked to me in my Spring context configuration

<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" id="pnxObjectMapper">
    <property name="deserializersByType">
        <map key-type="java.lang.Class">
            <entry>
                <key>
                    <value>org.joda.time.LocalDate</value>
                </key>
                <bean class="com.fasterxml.jackson.datatype.joda.deser.LocalDateDeserializer">
                    <constructor-arg>
                        <util:constant static-field="com.fasterxml.jackson.datatype.joda.cfg.FormatConfig.DEFAULT_DATETIME_PARSER" />
                    </constructor-arg>
                </bean>
            </entry>
        </map>
    </property>
</bean>

说明:加载JodaModule之前,它为 LocalDate定义了反序列化器正确采用了ISO日期格式,我强迫Spring在构造序列化程序时更改格式器

Explanation: before loading the JodaModule, which defines a deserializer for LocalDates rightfully adopting an ISO date-only format, I force Spring to change the formatter when constructing the serializer

这篇关于杰克逊将从ISO DateTime反序列化Joda LocalDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:45