jackson2 JSON ISO 8601来自JodaTime in Spring 3.2RC1 [英] jackson2 JSON ISO 8601 date from JodaTime in Spring 3.2RC1
问题描述
我正在使用Spring MVC 3.2RC1中的REST API。
I'm working on a REST API in Spring MVC 3.2RC1.
我正在获取一个带有org.joda.time.DateTime时间戳的JPA实体,让Spring使用
I'm fetching a JPA entity with a org.joda.time.DateTime timestamp in it and let Spring serialise it into JSON using
@RequestMapping(value = "/foobar", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
在Spring中使用默认的Jackson2设置,因为我只添加了
Using the default Jackson2 settings in Spring as I've only added
<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连接它自己。
to my POM and let Spring wire it up itself.
控制器正在生成:
"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(或带偏移量)。
But I would like it to be and ISO8601 date such as 2007-11-16T20:14:06.3Z (or with the offset).
我的猜测是我需要访问ObjectMapper和s et mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);但是如何在使用
My guess is that I need to access the ObjectMapper and set mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); But how do I get access to the ObjectMapper when using
<mvc:annotation-driven />
P.S。我使用UserType将对象持久化到PostgreSQL和JPA / Hibernate4以获得JodaTime支持。 DS
P.S. I'm persisting the objects to PostgreSQL with JPA/Hibernate4 using UserType to get JodaTime support. D.S.
更新
下面的配置解决了java.util.Date的问题但JodaTime仍然没有骰子。
The config below solves it for java.util.Date but still no dice for 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>
推荐答案
或者,如果您更喜欢用Java进行配置,它看起来像这样:
Or, if you prefer doing your configuration in Java, it could look like this:
@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());
}
}
这篇关于jackson2 JSON ISO 8601来自JodaTime in Spring 3.2RC1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!