如何解決SpringBoot版本升級引起數(shù)據(jù)顯示出錯及排查,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供紅花崗企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為紅花崗眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
Spring boot1.5.3
fastjson
<!--阿里 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",
經(jīng)過來回更換版本等幾個小時的嘗試后,分析結(jié)果:Spring Boot默認(rèn)采用jackson作為解析,原因可能是采用1.5.3時,WebMvcConfigurer extends WebMvcConfigurerAdapter類中關(guān)于fastjson的配置起了作用,解析框架采用了fastjson(@JSONField);而升級為2.0.6之后,由于沒有對WebMvcConfigurer配置(原WebMvcConfigurerAdapter上自動加了刪除線),Spring boot默認(rèn)采用了jackjson解析框架,導(dǎo)致@JSONField未起作用,故出現(xiàn)上述解析結(jié)果。
就是要自己定義解析框架fastjson,不用Spring boot默認(rèn)的jackson框架。
在啟動類中添加以下配置:
import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.context.annotation.Bean; import org.springframework.http.MediaType; @Bean public HttpMessageConverters fastJsonHttpMessageConverters(){ //創(chuàng)建FastJson信息轉(zhuǎn)換對象 FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); //創(chuàng)建Fastjosn對象并設(shè)定序列化規(guī)則 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // 中文亂碼解決方案 List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);//設(shè)定json格式且編碼為UTF-8 fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes); //規(guī)則賦予轉(zhuǎn)換對象 fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); return new HttpMessageConverters(fastJsonHttpMessageConverter); }
問題得到解決,時間格式可以正常返回顯示。
看完上述內(nèi)容,你們掌握如何解決SpringBoot版本升級引起數(shù)據(jù)顯示出錯及排查的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文名稱:如何解決SpringBoot版本升級引起數(shù)據(jù)顯示出錯及排查
標(biāo)題網(wǎng)址:http://bm7419.com/article30/gocspo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站改版、定制開發(fā)、網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)