思路
使用Spring的ResourceUtils讀取資源目錄下的json檔案。
使用common-io將讀取的檔案轉換為json字串。
使用fastjson將json字串反序列為物件。
範例
1.Maven依賴
pom.xml,主要是common-io、fastjson的引進。
<!-- 资源目录资源文件读取 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.11.0</version> </dependency> <!-- 反序列化json字符串 --> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.14</version> </dependency>
2.json資源檔案
notice.json,簡單列舉要使用json內容。
[ { "title": "新功能xxx上线", "content": "支持xxx" }, { "title": "旧功能xxx下线", "content": "不支持xxx" } ]
3.讀取json的Service
3.1.定義介面
package com.example.springbootjson.service; import com.example.springbootjson.domain.NoticeInfo; import java.io.IOException; import java.util.List; /** * @author hongcunlin */ public interface NoticeService { /** * 获取公告 * * @return 公告 * @throws IOException 文件 */ List<NoticeInfo> getNoticeInfoList() throws IOException; }
3.2.實作介面
這裡可以說是本文的核心部分了,具體可以看程式碼中的實現,透過ResourceUtils讀取notice.json這個json文件,透過common-io的FileUtils轉換文件為json字串,透過fastjson的JSON反序列json物件。
package com.example.springbootjson.service.impl; import com.alibaba.fastjson2.JSON; import com.example.springbootjson.domain.NoticeInfo; import com.example.springbootjson.service.NoticeService; import org.apache.commons.io.FileUtils; import org.springframework.stereotype.Service; import org.springframework.util.ResourceUtils; import java.io.File; import java.io.IOException; import java.util.List; /** * @author hongcunlin */ @Service public class NoticeServiceImpl implements NoticeService { @Override public List<NoticeInfo> getNoticeInfoList() throws IOException { File file = ResourceUtils.getFile("classpath:notice.json"); String json = FileUtils.readFileToString(file, "UTF-8"); List<NoticeInfo> noticeInfoList = JSON.parseArray(json, NoticeInfo.class); return noticeInfoList; } }
4.測試介面
寫一個簡單的整合測試,將上述編寫的Service注入,執行方法,列印執行結果。
package com.example.springbootjson; import com.example.springbootjson.service.NoticeService; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; import java.io.IOException; @SpringBootTest class SpringbootJsonApplicationTests { @Resource private NoticeService noticeService; @Test void contextLoads() throws IOException { System.out.println(noticeService.getNoticeInfoList()); } }
可以看到,可以正常地輸出json檔案中的內容,說明我們的程式是正確的。
以上是SpringBoot怎麼讀取資源目錄中JSON文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允許Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,對象與偏見,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的頂級功能包括:1)面向對象編程,支持多態性,提升代碼的靈活性和可維護性;2)異常處理機制,通過try-catch-finally塊提高代碼的魯棒性;3)垃圾回收,簡化內存管理;4)泛型,增強類型安全性;5)ambda表達式和函數式編程,使代碼更簡潔和表達性強;6)豐富的標準庫,提供優化過的數據結構和算法。

javaisnotirelyplatemententedduetojvmvariationsandnativecodinteinteration,butitlargelyupholdsitsitsworapromise.1)javacompilestobytecoderunbythejvm

thejavavirtualmachine(JVM)IsanabtractComputingmachinecrucialforjavaexecutionasitrunsjavabytecode,使“ writeononce,runanywhere”能力

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Java的五大特色是多態性、Lambda表達式、StreamsAPI、泛型和異常處理。 1.多態性讓不同類的對象可以作為共同基類的對象使用。 2.Lambda表達式使代碼更簡潔,特別適合處理集合和流。 3.StreamsAPI高效處理大數據集,支持聲明式操作。 4.泛型提供類型安全和重用性,編譯時捕獲類型錯誤。 5.異常處理幫助優雅處理錯誤,編寫可靠軟件。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具