2012-05-18 39 views
13

我有以下的串行处理JodaTime:@ResponseBody春杰克逊JsonSerializer与JodaTime

public class JodaDateTimeJsonSerializer extends JsonSerializer<DateTime> { 

    private static final String dateFormat = ("MM/dd/yyyy"); 

    @Override 
    public void serialize(DateTime date, JsonGenerator gen, SerializerProvider provider) 
      throws IOException, JsonProcessingException { 

     String formattedDate = DateTimeFormat.forPattern(dateFormat).print(date); 

     gen.writeString(formattedDate); 
    } 

} 

然后,在每个模型对象,我这样做:

@JsonSerialize(using=JodaDateTimeJsonSerializer.class) 
public DateTime getEffectiveDate() { 
    return effectiveDate; 
} 

通过上面的设置,@ResponseBody和杰克逊Mapper肯定有效。不过,我不喜欢这个想法,我一直在写@JsonSerialize。我需要的是在模型对象上没有@JsonSerialize的解决方案。是否可以在xml中将这个配置写入一个配置中?

感谢您的帮助。

回答

10

尽管您可以为每个日期字段添加注释,但最好为对象映射器进行全局配置。如果你使用杰克逊,你可以配置你的春天如下:

<bean id="jacksonObjectMapper" class="com.company.CustomObjectMapper" /> 

<bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig" 
    factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" > 
</bean> 

对于CustomObjectMapper:

public class CustomObjectMapper extends ObjectMapper { 

    public CustomObjectMapper() { 
     super(); 
     configure(Feature.WRITE_DATES_AS_TIMESTAMPS, false); 
     setDateFormat(new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'ZZZ (z)")); 
    } 
} 

当然,SimpleDateFormat的可以使用任何你需要的格式。

+0

'setDateFormat(new SimpleDateFormat(“yyyy-MM-dd'T'HH:mm:ssz”));'为我做了诀窍,thx – Konsumierer

-3

如果你只在你的类路径上有Jackson JAR,并返回@ResponseBody,Spring将自动将Model对象转换为JSON。您无需在模型中注释任何内容以使其发挥作用。

+0

嗯,我试过了。当杰克逊将对象转换为JSON时,它将JodaTime实例转换为毫秒内的字符串。这就是为什么自定义转换器。我真的想避免在我所有的日期变量中使用@JsonSerialize。我需要一种方式在应用程序级别进行配置。 –

+0

看起来我的假设是错误的。实际上有一个相关的Spring bug(https://jira.springsource.org/browse/SPR-6731),但即使在那里,它也可以使默认注释生效。所以,看起来你需要在每个模型类中进行注释。 – atrain

+0

我明白。不知道是否有人成功配置这种AOP风格。让我们等待其他人有任何意见。 –

1

@Moesio几乎明白了。下面是我的配置:

<!-- Configures the @Controller programming model --> 
<mvc:annotation-driven/> 

<!-- Instantiation of the Default serializer in order to configure it --> 
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapterConfigurer" init-method="init"> 
    <property name="messageConverters"> 
     <list> 
      <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
       <property name="objectMapper" ref="jacksonObjectMapper" /> 
      </bean> 
     </list> 
    </property> 
</bean> 

<bean id="jacksonObjectMapper" class="My Custom ObjectMapper"/> 

<bean id="jacksonSerializationConfig" class="org.codehaus.jackson.map.SerializationConfig" 
    factory-bean="jacksonObjectMapper" factory-method="getSerializationConfig" /> 

这让我是<mvc:annotation-driven/>使得自己AnnotationMethodHandler,而忽略你手动进行的一个位。我从http://scottfrederick.blogspot.com/2011/03/customizing-spring-3-mvcannotation.html获得了BeanPostProcessing的想法来配置一个被使用的想法,并且瞧!奇迹般有效。

0

同样使用Spring 3 JavaConfig:

@Configuration 
@ComponentScan() 
@EnableWebMvc 
public class WebConfig extends WebMvcConfigurerAdapter 
{ 

    @Override 
    public void configureMessageConverters(final List<HttpMessageConverter<?>> converters) 
    { 
     converters.add(0, jsonConverter()); 
    } 

    @Bean 
    public MappingJacksonHttpMessageConverter jsonConverter() 
    { 
     final MappingJacksonHttpMessageConverter converter = new MappingJacksonHttpMessageConverter(); 
     converter.setObjectMapper(new CustomObjectMapper()); 

     return converter; 
    } 
}