在Spring 4.1.3中注册ObjectMapper以序列化Joda DateTime [英] Register ObjectMapper in Spring 4.1.3 to serialize Joda DateTime
问题描述
jackson-datatype-joda
,它的模块 JodaModule
所以我添加了依赖关系: <依赖关系>
< groupId> com.fasterxml.jackson.datatype< / groupId>
< artifactId> jackson-datatype-joda< / artifactId>
< version> 2.4.4< / version>
< / dependency>
我的弹簧配置:
< bean id =objectMapper
class =org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean
p:indentOutput =true>
< property name =featuresToDisable>
< array>
< util:constant static-field =com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS/>
< / array>
< / property>
< property name =modulesToInstallvalue =com.fasterxml.jackson.datatype.joda.JodaModule/>
< / bean>
< mvc:注释驱动>
< mvc:message-converters>
< bean class =org.springframework.http.converter.json.MappingJackson2HttpMessageConverter>
< property name =objectMapperref =objectMapper/>
< / bean>
< / mvc:message-converters>
< / mvc:注释驱动>
当我尝试序列化这个bean
public class Bean {
private DateTime start = new DateTime();
public DateTime getStart(){return start; }
public void setStart(DateTime start){this.start = start;
}
我得到以下输出,但是希望它在ISO 8601格式:
{开始:1418337158933}
我发现如果在classpath中找到了 JodaModule
也可以预加载,所以不需要手动注册它(请参阅 github repo ),但这个代码在应用程序启动期间被调用了很多次。
我认为原因是 ObjectMapper
被实例化在某些其他地方。
更新:问题是,在声明了ObjectMapper的Spring配置中还有一个文件。答案中提供的两个解决方案都可以正常工作。干杯!
如果您在下面的代码中创建自定义的ObjectMapper类,它将覆盖默认的objectMapper,并且不需要您的xml配置完全是:
@Service
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper {
this.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,false);
this.registerModule(new JodaModule());
}
}
我将这个简单的服务添加到简单的弹簧启动应用程序并获得预期格式的时间。
I'm trying to configure ObjectMapper in my Web application to serialize/deserialise dates presented as Joda's DateTime in ISO 8601 format. I found useful library jackson-datatype-joda
and it's module JodaModule
so I've added dependency:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.4.4</version>
</dependency>
My Spring configuration:
<bean id="objectMapper"
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
p:indentOutput="true">
<property name="featuresToDisable">
<array>
<util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS" />
</array>
</property>
<property name="modulesToInstall" value="com.fasterxml.jackson.datatype.joda.JodaModule" />
</bean>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
When I try to serialize this bean
public class Bean {
private DateTime start = new DateTime();
public DateTime getStart() { return start; }
public void setStart(DateTime start) { this.start = start; }
}
I get following output as long but instead want it to be in ISO 8601 format:
{"start":1418337158933}
I found that JodaModule
also preloads if it is found in classpath so it is not neccessary to register it manually (see github repo) but this code is invoked many times during application start.
I think the reason is that ObjectMapper
is instantiated in some other place.
UPDATE: The problem was that there was one more file with Spring configuration where ObjectMapper was declared. Both solutions given in answers will work. Cheers!
If you create you custom ObjectMapper class like in the code below it will override default objectMapper and you don't need your xml configuration at all:
@Service
public class CustomObjectMapper extends ObjectMapper {
public CustomObjectMapper() {
this.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
this.registerModule(new JodaModule());
}
}
I've added this simple service to the simple spring boot application and got time in the expected format.
这篇关于在Spring 4.1.3中注册ObjectMapper以序列化Joda DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!