欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot讀取資源目錄中JSON文件的方法實例

 更新時間:2023年04月11日 10:27:37   作者:肥肥技術(shù)宅  
最近做項目遇到需要將json類型的配置文件引用到項目中,已經(jīng)將讀取json文件的方法封裝成工具類,下面這篇文章主要給大家介紹了關(guān)于SpringBoot讀取資源目錄中JSON文件的相關(guān)資料,需要的朋友可以參考下

前言

最近在做一個公共相關(guān)的內(nèi)容,公告里邊的內(nèi)容,打算做成配置化的。

但是考慮到存儲到數(shù)據(jù)庫,需要建立數(shù)據(jù)庫表;

存儲到配置組件中,擔(dān)心配置組件存儲不下;

于是決定先暫時存儲到項目中的資源目錄中,以JSON的格式存儲,待觀察公告這一模塊的需求變更如何,再另行做打算。

本文分享SpringBoot讀取資源目錄JSON配置文件的相關(guān)方法。

思路

使用Spring的ResourceUtils讀取資源目錄下的json文件。

使用common-io將讀取的文件轉(zhuǎn)化為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內(nèi)容。

[
  {
    "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.實現(xiàn)接口

這里可以說是本文的核心部分了,具體可以看代碼中的實現(xiàn),通過ResourceUtils讀取notice.json這個json文件,通過common-io的FileUtils轉(zhuǎn)化文件為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注入,執(zhí)行方法,打印執(zhí)行結(jié)果。

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文件中的內(nèi)容,說明我們的程序是正確的。

最后

本文分享了SpringBoot工程讀取項目資源目錄下的文件的相關(guān)方法,分享的原因,是看到很多人使用了原始的文件IO的API,這沒必要,SpringBoot已經(jīng)為我們封裝提供好了很多優(yōu)雅的API了。作為開發(fā)者,連API的使用,我們也得與時俱進。

到此這篇關(guān)于SpringBoot讀取資源目錄中JSON文件的文章就介紹到這了,更多相關(guān)SpringBoot讀取資源JSON文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論