Spring boot1.5.3spring
fastjsonjson
<!--阿里 FastJson依賴--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
import com.alibaba.fastjson.annotation.JSONField; import org.springframework.format.annotation.DateTimeFormat; @JSONField(format = "yyyy-MM-dd HH:mm") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") private Date pubTime;
"pubTime": "2019-02-19 13:45",
"pubTime": "2019-02-26T09:22:24.000+0000",
通過來回更換版本等幾個小時的嘗試後,分析結果:Spring Boot默認採用jackson做爲解析,緣由多是採用1.5.3時,WebMvcConfigurer extends WebMvcConfigurerAdapter類中關於fastjson的配置起了做用,解析框架採用了fastjson(@JSONField);而升級爲2.0.6以後,因爲沒有對WebMvcConfigurer配置(原WebMvcConfigurerAdapter上自動加了刪除線),Spring boot默認採用了jackjson解析框架,致使@JSONField未起做用,故出現上述解析結果。框架
就是要本身定義解析框架fastjson,不用Spring boot默認的jackson框架。學習
在啓動類中添加如下配置:測試
import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.http.MediaType; @Bean public HttpMessageConverters fastJsonHttpMessageConverters(){ //建立FastJson信息轉換對象 FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); //建立Fastjosn對象並設定序列化規則 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // 中文亂碼解決方案 List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);//設定json格式且編碼爲UTF-8 fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes); //規則賦予轉換對象 fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); return new HttpMessageConverters(fastJsonHttpMessageConverter); }
問題獲得解決,時間格式能夠正常返回顯示。編碼
掌握jackson、fastjson與gson三種解析框架的區別。code
參考連接orm
https://www.jianshu.com/p/45c603e34203