SpringBoot强制设置Jackson时区

@Configuration
public class JacksonConfig extends WebMvcConfigurationSupport {

    @Bean
    public ObjectMapper objectMapper() {

        Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = new Jackson2ObjectMapperBuilder()
                .timeZone(TimeZone.getTimeZone("Asia/Shanghai"));

        return jackson2ObjectMapperBuilder.build();
    }

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 已有的MappingJackson2HttpMessageConverter bean有可能时区不是gmt+8,直接干掉,然后添加一个新的
        converters.removeIf(clazz -> clazz instanceof MappingJackson2HttpMessageConverter);
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper()));
    }
}

 

posted @ 2021-05-31 11:18  Joynic  阅读(1331)  评论(0编辑  收藏  举报