spring-boot 版本java
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> </parent>
spring-boot 項目中 全局配置 Date類型返回指定時間格式 以下:web
# jackson時間格式化 spring.jackson.time-zone=GMT+8 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
測試沒有做用, 緣由:spring
package com.richfun.boot.common.config; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.richfun.boot.common.util.DateUtil; import com.richfun.boot.common.util.UploadUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.text.SimpleDateFormat; import java.util.List; /** * WebMvc配置 * @author geYang 2018-05-14 */ @Configuration public class WebConfig implements WebMvcConfigurer { /** * 使用此方法, 如下 spring-boot: jackson時間格式化 配置 將會失效 * spring.jackson.time-zone=GMT+8 * spring.jackson.date-format=yyyy-MM-dd HH:mm:ss * 緣由: 會覆蓋 @EnableAutoConfiguration 關於 WebMvcAutoConfiguration 的配置 * */ @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = converter.getObjectMapper(); // 生成JSON時,將全部Long轉換成String 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(DateUtil.PATTERN_TIME)); // 設置格式化內容 converter.setObjectMapper(objectMapper); converters.add(0, converter); } }
解決方案: 使用如上方法 extendMessageConverters 統一解決apache