在spring中配置time-zone=GMT+8无效、不生效解决方案

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

spring.jackson.time-zone=GMT+8

不起作用???不生效???why???

 

需要实现 WebMvcConfigurer 或 继承WebMvcConfigurerAdapter

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {
    //解决 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
    //spring.jackson.time-zone=GMT+8 不生效的功能
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = converter.getObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        converter.setObjectMapper(objectMapper);
        converters.add(0, converter);
    }
}

 

posted @ 2022-04-24 20:56  韦邦杠  阅读(2832)  评论(0编辑  收藏  举报