helpme7766
helpme7766

Reputation: 269

Jackson Serialize date as timestamps

I have upgraded my spring boot project from 1.5.8 to 2.0.4. I have noticed that previously all our dates were getting returned as timestamps which was fine. since the upgrade the dates were coming out as dates, in order to get them back out as timestamps again i added the following to my yml file:

jackson:
  serialization:
    write-dates-as-timestamps: true

Now all the dates are getting returned as dates. Can't figure out why. We have a configuration class which has a jacksonMessageConverter bean defined (required for when converting messages of a rabbitMq), not sure if this is messing up the conversion somehow?

Any help much appreciated

public class ApplicationConfig{
    @Bean
    public AuditorAware<String> auditorProvider() {
        return new AuditorAwareImpl();
    }

    @Bean
    public Jackson2JsonMessageConverter jackson2JsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new 
        CachingConnectionFactory(<host>);
        connectionFactory.setUsername(<user>);
        connectionFactory.setPassword(<pas>);
        connectionFactory.setVirtualHost(<vhost>);
        return connectionFactory;
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory());
    }

Update:

I have since added the the following the my config class and now it is working, any ideas why I need this? And why it hasn't just picked up from the spring boot properties?

@Bean
    public Jackson2ObjectMapperBuilder objectMapperBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.featuresToEnable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        return builder;
    }

It doesn't work on date fields where I have defined the format as such, how can I resolve this?:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")  

Upvotes: 2

Views: 5167

Answers (1)

alturkovic
alturkovic

Reputation: 1140

Shouldn't it be:

spring:
  jackson:
    serialization:
      write-dates-as-timestamps: true

Aren't you missing the spring prefix?

Upvotes: 4

Related Questions