正常來講,在專案中用到訊息日期這類資訊的時候,如果要按要求格式化,一般有兩種方法
1. @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
- 在springboot的通常配置攔截器的WebMvcConfiguration中擴充套件SpringMVC的訊息轉換器,統一對日期型別進行格式化處理(推薦)
第一步:設定相關依賴(負責json的)
```
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
```
第二步,佈置訊息轉換器這個工具類
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
/**
* 物件對映器:基於jackson將Java物件轉為json,或者將json轉為Java物件
* 將JSON解析為Java物件的過程稱為 [從JSON反序列化Java物件]
* 從Java物件生成JSON的過程稱為 [序列化Java物件到JSON]
*/
public class JacksonObjectMapper extends ObjectMapper {
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
//public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm";
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
public JacksonObjectMapper() {
super();
//收到未知屬性時不報異常
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
//反序列化時,屬性不存在的相容處理
this.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
SimpleModule simpleModule = new SimpleModule()
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)))
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)))
.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)))
.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
//註冊功能模組 例如,可以新增自定義序列化器和反序列化器
this.registerModule(simpleModule);
}
}
第三步 攔截器重寫的方法
@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//建立一個訊息轉換器物件
MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter();
//需要為訊息轉換器設定一個物件轉換器,物件轉換器可以將java物件序列化為json資料
converter.setObjectMapper(new JacksonObjectMapper());
//新增訊息轉換器到攔截器,並透過序號提升優先順序
converters.add(0,converter);
}