使用Java配置配置MapJackson2HttpMessageConverter

wh6knrhe  于 2022-11-08  发布在  Java
关注(0)|答案(4)|浏览(160)

我正在尝试配置MappingJackson 2 HttpMessageConverter,因为我希望禁用WRITE_DATES_TO_TIMESTAMP功能。
如果我用xml-configuration配置它,它就能成功运行

<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>

但是,当我尝试通过java-configuration进行配置时,转换器已注册,但序列化忽略了Map器的配置。
我尝试过不同的方法。
方法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));
}
}

方法二

@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;
        }
    }
}

}
mf98qq94

mf98qq941#

请尝试以下方法:

@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()));
    }
    ...
}
zxlwwiss

zxlwwiss2#

也许你仍然有xml中的注解驱动?
它还注册默认的messageConverter和objectmapper。

o4tp2gmn

o4tp2gmn3#

这也适用于我

@Bean
public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    return builder;
}
bgibtngc

bgibtngc4#

试试看:

@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());
}
}

相关问题