SpringBoot读取资源目录中JSON文件的方法实例
作者:肥肥技术宅 时间:2023-04-26 02:00:42
前言
最近在做一个公共相关的内容,公告里边的内容,打算做成配置化的。
但是考虑到存储到数据库,需要建立数据库表;
存储到配置组件中,担心配置组件存储不下;
于是决定先暂时存储到项目中的资源目录中,以JSON的格式存储,待观察公告这一模块的需求变更如何,再另行做打算。
本文分享SpringBoot读取资源目录JSON配置文件的相关方法。
思路
使用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工程读取项目资源目录下的文件的相关方法,分享的原因,是看到很多人使用了原始的文件IO的API,这没必要,SpringBoot已经为我们封装提供好了很多优雅的API了。作为开发者,连API的使用,我们也得与时俱进。
来源:https://blog.csdn.net/m0_71777195/article/details/127050596
标签:springboot,读取,json文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#如何使用Task执行异步操作
2023-01-12 03:34:41
java实现归并排序算法
2023-02-09 07:34:01
![](https://img.aspxhome.com/file/2023/3/77063_0s.jpg)
jvm垃圾回收之GC调优工具分析详解
2023-08-17 12:40:20
![](https://img.aspxhome.com/file/2023/5/111455_0s.png)
使用 C# 下载文件的多种方法小结
2023-11-08 06:59:37
![](https://img.aspxhome.com/file/2023/7/72457_0s.gif)
Android 获取屏幕尺寸实例代码
2023-07-26 21:50:47
C#中ExecuteNonQuery()返回值注意点分析
2023-04-05 03:40:14
Android数据持久化之Preferences机制详解
2021-09-04 23:42:32
DataGridView不显示最下面的新行、判断新增行、删除行操作
2022-11-26 16:14:36
Android实现显示和隐藏密码功能的示例代码
2022-12-14 23:40:09
![](https://img.aspxhome.com/file/2023/0/101870_0s.png)
JDBC自定义连接池过程详解
2023-11-17 13:27:55
解析在内部循环中Continue外部循环的使用详解
2023-09-24 08:03:50
详解ThreadLocal为什么会内存溢出原理
2023-11-09 18:45:26
![](https://img.aspxhome.com/file/2023/3/59183_0s.png)
解决IDEA中快捷键Alt+Enter不能使用的问题
2022-09-14 07:54:07
![](https://img.aspxhome.com/file/2023/8/91128_0s.png)
Java读取本地json文件及相应处理方法
2023-10-16 16:37:34
JAVA中的Token 基于Token的身份验证实例
2023-11-09 18:05:09
Android 异步获取网络图片并处理导致内存溢出问题解决方法
2022-10-11 07:58:14
Android使用Item Swipemenulistview实现仿QQ侧滑删除功能
2021-08-16 19:47:35
![](https://img.aspxhome.com/file/2023/6/138546_0s.jpg)
Android中使用TextView实现高仿京东淘宝各种倒计时效果
2021-05-26 21:32:26
![](https://img.aspxhome.com/file/2023/6/139136_0s.png)
使用C++程序获取新浪行情数据的方法
2022-06-16 13:14:35
![](https://img.aspxhome.com/file/2023/5/117755_0s.png)
Java统计输入字符的英文字母、空格、数字和其它
2022-11-13 04:11:31