SpringBoot全局时间格式设置

springboot返回到前端时间格式为时间戳

yaml配置文件中新增配置。

spring:
jackson:
default-property-inclusion: ALWAYS
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
serialization:
write-dates-as-timestamps: false

新增配置类。

  • 继承 WebMvcConfigurerAdapter 或者 WebMvcConfigurationSupport 或者 实现WebMvcConfigurer,但是WebMvcConfigurerAdapter 废弃了 *

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

@Override
public void configureMessageConverters(List> converters){
    MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    jackson2HttpMessageConverter.setObjectMapper(objectMapper);
    converters.add(jackson2HttpMessageConverter);
}

}

你可能感兴趣的:(SpringBoot,java,spring)