14

我正在使用 Spring MVC 3.2RC1 中的 REST API。

我正在获取一个带有 org.joda.time.DateTime 时间戳的 JPA 实体,并让 Spring 使用将其序列化为 JSON

@RequestMapping(value = "/foobar", method = RequestMethod.GET, produces = "application/json")
@ResponseBody

在 Spring 中使用默认的 Jackson2 设置,因为我只添加了

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.1.1</version>
</dependency>

到我的 POM 并让 Spring 自行连接。

控制器正在生成:

"created":{"year":2012,"dayOfMonth":30,"dayOfWeek":5,"era":1,"dayOfYear":335,"weekOfWeekyear":48,"weekyear":2012,"monthOfYear":11,"yearOfEra":2012,"yearOfCentury":12,"centuryOfEra":20,"millisOfSecond":39,"millisOfDay":52684039,"secondOfMinute":4,"secondOfDay":52684,"minuteOfHour":38,"minuteOfDay":878,"hourOfDay":14,"millis":1354282684039,"zone":{"uncachedZone":{"cachable":true,"fixed":false,"id":"Europe/Stockholm"},"fixed":false,"id":"Europe/Stockholm"},"chronology":{"zone":{"uncachedZone":{"cachable":true,"fixed":false,"id":"Europe/Stockholm"},"fixed":false,"id":"Europe/Stockholm"}},"afterNow":false,"beforeNow":true,"equalNow":false}

但我希望它是 ISO8601 日期,例如 2007-11-16T20:14:06.3Z (或带有偏移量)。

我的猜测是我需要访问 ObjectMapper 并设置 mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); 但是如何在使用时访问 ObjectMapper

<mvc:annotation-driven />

PS 我使用 UserType 将对象持久化到带有 JPA/Hibernate4 的 PostgreSQL 以获得 JodaTime 支持。DS

更新

下面的配置解决了 java.util.Date 的问题,但 JodaTime 仍然没有骰子。

<annotation-driven>
    <message-converters>
        <beans:bean
            class="org.springframework.http.converter.StringHttpMessageConverter" />
        <beans:bean
            class="org.springframework.http.converter.ResourceHttpMessageConverter" />
        <beans:bean
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <beans:property name="objectMapper">
                <beans:bean
                    class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
                    p:indentOutput="true" p:simpleDateFormat="yyyy-MM-dd'T'HH:mm:ss.SSSZ">
                </beans:bean>
            </beans:property>
        </beans:bean>
    </message-converters>
</annotation-driven>
4

5 回答 5

13

或者,如果您更喜欢用 Java 进行配置,它可能如下所示:

@Configuration
@EnableWebMvc
public class RestConfig extends WebMvcConfigurerAdapter {

    private ObjectMapper objectMapper() {
        Jackson2ObjectMapperFactoryBean bean = new Jackson2ObjectMapperFactoryBean();
        bean.setIndentOutput(true);
        bean.setSimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        bean.afterPropertiesSet();
        ObjectMapper objectMapper = bean.getObject();
        objectMapper.registerModule(new JodaModule());
        return objectMapper;
    }

    private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(objectMapper());
        return converter;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(mappingJackson2HttpMessageConverter());
    }

}
于 2013-04-25T19:22:43.473 回答
11

只是为了总结答案并发布 Spring 中 JodaTime 序列化的工作解决方案(在 Spring 3.2 上测试)

弹簧上下文.xml

<bean id="objectMapper"
    class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
    p:indentOutput="true" p:simpleDateFormat="yyyy-MM-dd'T'HH:mm:ss.SSSZ">
</bean>
<bean
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
    p:targetObject-ref="objectMapper" p:targetMethod="registerModule">
    <property name="arguments">
        <list>
            <bean class="com.fasterxml.jackson.datatype.joda.JodaModule" />
        </list>
    </property>
</bean>

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
        <bean
            class="org.springframework.http.converter.ResourceHttpMessageConverter" />

        <bean
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="objectMapper" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

Maven 依赖项(com.fasterxml.jackson-version 为 2.1.1)

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${com.fasterxml.jackson-version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${com.fasterxml.jackson-version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-joda</artifactId>
    <version>${com.fasterxml.jackson-version}</version>
</dependency>

在此之后,您在 ResponseBody 中的 JodaTime 字段将在 JSON 中自动序列化为“createdDate”:“2013-01-18T15:15:36.365+02:00”

于 2013-01-18T13:26:37.757 回答
11

我最终使用jackson-datatype-joda让它工作:

添加另一个 Maven 依赖项(匹配您的 Jackson 版本号):

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-joda</artifactId>
    <version>${jackson.version}</version>
</dependency>

然后将 JodaModule(处理转换)注册到 Jackson 的 ObjectMapper(这里在 Spring 中完成,但您可以创建一个辅助类):

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
    p:targetObject-ref="objectMapper" p:targetMethod="registerModule">
    <property name="arguments">
        <list><bean class="com.fasterxml.jackson.datatype.joda.JodaModule"/></list>
    </property>
</bean>

(您需要为 ObjectMapper 提供一个 id,以便可以通过这种方式引用它)。

Hibernate模块也是这样注册的:https ://github.com/FasterXML/jackson-module-hibernate

请注意,您需要设置一个(简单)日期格式,如问题所示,但禁用SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS似乎没有任何区别。

于 2013-01-06T18:04:34.543 回答
2

我正在努力解决杰克逊从实体字段与 joda DayTime 创建的完全相同的 b@#$d :

modifiedOn": {

"year": 2013,
"dayOfWeek": 6,
"era": 1,
"dayOfYear": 124,
"dayOfMonth": 4,
"weekOfWeekyear": 18,
"monthOfYear": 5,
"yearOfCentury": 13,
"centuryOfEra": 20,
"millisOfSecond": 0,
"millisOfDay": 81801000,
"secondOfMinute": 21,
"secondOfDay": 81801,
"minuteOfHour": 43,
"minuteOfDay": 1363,
"weekyear": 2013,
"yearOfEra": 2013,
"hourOfDay": 22,
"millis": 1367700201000,
"zone": {
    "uncachedZone": {
        "cachable": true,
        "fixed": false,
        "id": "Europe/Belgrade"
    },
    "fixed": false,
    "id": "Europe/Belgrade"
},
"chronology": {
    "zone": {
        "uncachedZone": {
            "cachable": true,
            "fixed": false,
            "id": "Europe/Belgrade"
        },
        "fixed": false,
        "id": "Europe/Belgrade"
    }
},
"afterNow": false,
"beforeNow": true,
"equalNow": false
}

正如这里提到的 https://github.com/FasterXML/jackson-datatype-joda 使用 Jackson 2.0 非常容易,但对于像我这样的新手来说,很难弄清楚如何让它工作,但最终找到了代码:

maven 中的依赖 - 这很简单

<dependency>
 <groupId>com.fasterxml.jackson.datatype</groupId>
 <artifactId>jackson-datatype-joda</artifactId>
 <version>2.1.2</version>
</dependency>

以及 FasterXML 文档中的一些代码

objectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());

...但是如何实施呢?这是示例 - 新类:

public class JodaObjectMapper extends ObjectMapper {

    public JodaObjectMapper() {
        super();
        registerModule(new JodaModule());
    }
}

最后一步 - 除了 spring.xml

<mvc:annotation-driven>
 <mvc:message-converters register-defaults="true">
 <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
 <property name="objectMapper">
 <bean class="net.jvw.JodaObjectMapper"/>
 </property>
 </bean>
 </mvc:message-converters>
</mvc:annotation-driven>

所以现在让我们看看生成的 json

"modifiedOn": 1367701129075

终于有一些容易处理的事情了

来自博客文章http://vanwilgenburg.wordpress.com/2013/01/14/return-usable-joda-dates-in-json-with-jackson/

如果有人想查看我的实体类或控制器的更多代码 - 请在评论中提问,我将在此答案中添加足够的代码。

于 2013-05-04T21:37:34.567 回答
0

我一直在努力解决同样的问题,并尝试以这种方式将 DateTime 的 Spring 序列化简化为 JSON app-servlet.xml

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean class="no.bouvet.jsonclient.spring.JsonClientJackson2ObjectMapperFactoryBean"/>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

在这里,您可以从java-json-client库中找到no.bouvet.jsonclient.spring.JsonClientJackson2ObjectMapperFactoryBean

于 2014-10-15T19:01:14.117 回答