JEET ADHIKARI
JEET ADHIKARI

Reputation: 539

How to stop jackson from writing dates as timestamp in SpringMVC

I have been going through a tutorial on Spring boot and there I learnt how to stop jackson from converting dates to timestamp. eg :

{"birthDate":1505736233603} //before
{"birthDate":"2017-09-18T12:04:27.345+0000"}//after

by writing

 spring.jackson.serialization.write-dates-as-timestamps=false

in application.properties.

How do I do the same for SpringMVC, ofcourse there is no application.properties in SpringMVC

Upvotes: 8

Views: 23530

Answers (3)

Fervento
Fervento

Reputation: 314

Another way is to configure the MessageConverter after they have been populated/configured by the framework:

@EnableWebMvc
@Configuration
public class AppConfiguration implements WebMvcConfigurer {
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (HttpMessageConverter<?> converter : converters) {
            if (converter instanceof AbstractJackson2HttpMessageConverter) {
                AbstractJackson2HttpMessageConverter jacksonconverter = (AbstractJackson2HttpMessageConverter) converter;
                jacksonconverter.getObjectMapper()
                        .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
            }
        }
    }
}

Moreover, this code should also apply to SMILE because MappingJackson2SmileHttpMessageConverter extends AbstractJackson2HttpMessageConverter.

Upvotes: 8

Ricky Chen
Ricky Chen

Reputation: 41

if you have access to the ObjectMapper, you can also set it as a property programmatically

ObjectMapper mapper = new ObjectMapper();
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.setDateFormat(new ISO8601DateFormat());

Upvotes: 4

j_perez
j_perez

Reputation: 106

You'll have to configure the Spring Bean in charge of creating the JSON your service is returning.

First off, you need to define the Jackson Object Mapper Bean that your converter will use to create the JSON:

<bean class="com.fasterxml.jackson.databind.ObjectMapper" id="objectMapper">
    <property name="dateFormat">
        <bean class="java.text.SimpleDateFormat">
            <constructor-arg value="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"/>
        </bean>
    </property>
</bean>

(NOTE that you can define the dateFormat that you need).

Then, you need to inject this objectMapper bean into the JSON Message converter:

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

    </mvc:message-converters>
</mvc:annotation-driven>    

As you can see, I'm using the "mvc" namespace ("http://www.springframework.org/schema/mvc") to define the MVC beans.

If you're using Annotations rather than XML configuration, you can do exactly the same by defining the next Configuration class (or adapt it to you code :) )

@EnableWebMvc
@Configuration
@ComponentScan({ "com.yourorg.app" })
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(
        List<HttpMessageConverter<?>> converters) {
        messageConverters.add(new createJsonHttpMessageConverter()); 
        super.configureMessageConverters(converters);
    }

    private HttpMessageConverter<Object> createJsonHttpMessageConverter() {

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));

        MappingJackson2HttpMessageConverter jsonConverter = 
           new MappingJackson2HttpMessageConverter();
        jsonConverter.setObjectMapper(objectMapper);

        return jsonConverter;
    }
}

Hope this helps :)

Upvotes: 9

Related Questions