前言
最近在做一个公共相关的内容,公告里边的内容,打算做成配置化的。
但是考虑到存储到数据库,需要建立数据库表;
存储到配置组件中,担心配置组件存储不下;
于是决定先暂时存储到项目中的资源目录中,以JSON的格式存储,待观察公告这一模块的需求变更如何,再另行做打算。
本文分享SpringBoot读取资源目录JSON配置文件的相关方法。
思路
使用Spring的ResourceUtils读取资源目录下的json文件。
使用common-io将读取的文件转化为json字符串。
使用fastjson将json字符串反序列为对象。
1.Maven依赖
pom.xml,主要是common-io、fastjson的引入。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!-- 资源目录资源文件读取 --> <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内容。
?
1 2 3 4 5 6 7 8 9 10 | [ { "title": "新功能xxx上线", "content": "支持xxx" }, { "title": "旧功能xxx下线", "content": "不支持xxx" }] |
3.读取json的Service
3.1.定义接口
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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对象。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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测试数据mons.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 */@Servicepublic 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注入,执行方法,打印执行结果。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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;@SpringBootTestclass SpringbootJsonApplicationTests { @Resource private NoticeService noticeService; @Test void contextLoads() throws IOException { System.out.println(noticeService.getNoticeInfoList()); }} |
可以看到,可以正常地输出json文件中的内容,说明我们的程序是正确的。
最后
本文分享了SpringBoot工程读取项目资源目录下的文件的相关方法,分享的原因,是看到很多人使用了原始的文件IO的API,这没必要,SpringBoot已经为我们封装提供好了很多优雅的API了。作为开发者,连API的使用,我们也得与时俱进。
到此这篇关于SpringBoot读取资源目录中JSON文件的文章就介绍到这了,更多相关SpringBoot读取资源JSON文件内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/m0_71777195/article/details/127050596
查看更多关于SpringBoot读取资源目录中JSON文件的方法实例的详细内容...