Configure MappingJackson2HttpMessageConverter by using java-configuration
Asked Answered
S

4

6

I am trying to configure MappingJackson2HttpMessageConverter because I want to disable the WRITE_DATES_TO_TIMESTAMP feature.

If I configure it by xml-configuration it works successfully

<bean name="jacksonObjectMapper"
    class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
    <property name="featuresToDisable">
        <array>
            <util:constant
                static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS" />
        </array>
    </property>
</bean>
<mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <bean
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper" ref="jacksonObjectMapper" />
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

But when I try to configure through java-configuration the converter is registered but the serialization ignores the configuration of the mapper.

I have tried different aproaches.

Approach 1

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    converters.add(new MappingJackson2HttpMessageConverter(mapper));
}
}

Approach 2

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    for (HttpMessageConverter<?> converter : converters) {
        if (converter instanceof MappingJackson2HttpMessageConverter) {
        MappingJackson2HttpMessageConverter jacksonMessageConverter = (MappingJackson2HttpMessageConverter) converter;
        ObjectMapper objectMapper = jacksonMessageConverter.getObjectMapper();
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        break;
        }
    }
}

}
Susann answered 28/4, 2016 at 6:27 Comment(0)
M
8

Try the below approach:

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    ...
    @Override
    public void configureMessageConverters(
        List<HttpMessageConverter<?>> converters) {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.dateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm"));
        builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
    }
    ...
}
Morphia answered 28/4, 2016 at 6:30 Comment(1)
This worked for me for spring 5.2, thanks!Deflocculate
P
1

Maybe you still have annotation-driven in the xml?

Which also registers default messageConverters and objectmappers.

Periderm answered 23/1, 2017 at 6:14 Comment(1)
Removing <mvc:annotation-driven /> did the trick for me. Mixing xml config with annotations is not ideal, but something that's difficult to avoid in a legacy project.Tabaret
R
1

Try this:

@Configuration
@EnableWebMvc
@ComponentScan
public class MvcConfig implements WebMvcConfigurer {
    @Bean
    public MappingJackson2HttpMessageConverter jsonConverter() {
    List<MediaType> supportedMediaTypes = new ArrayList<>();
    supportedMediaTypes.add(MediaType.APPLICATION_JSON);

    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.serializationInclusion(JsonInclude.Include.NON_NULL);

    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(builder.build());
    jsonConverter.setSupportedMediaTypes(supportedMediaTypes);
    return jsonConverter;
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new StringHttpMessageConverter());
    converters.add(jsonConverter());
}
}
Reggie answered 26/6, 2022 at 23:17 Comment(0)
B
0

This also works for me

@Bean
public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    return builder;
}
Baldric answered 28/4, 2016 at 7:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.