一、spring 读取 resource文件数据
1、创建resource目录的文件 /data/oftenQuestion.json
String path = “/data/oftenQuestion.json”; // 该文件是一个 数组对象文件
2、读取文件
InputStream config = getClass().getResourceAsStream(path);
3、进行转换 到 数组对象
ObjectMapper mapper = new ObjectMapper(); // 这个对象 可以转换
result = mapper.readValue(config, List.class); // 转换成功,记得加上异常处理哦
本次记录一下,方便以后使用
完整代码贴
String path = "/data/oftenQuestion.json";
InputStream config = getClass().getResourceAsStream(path);
if (config == null) {
return ClientResponseFactory.create(ErrorEnum.DATA_PARSE_ERROR);
} else {
ObjectMapper mapper = new ObjectMapper();
List<OftenQuestionAo> result;
try {
result = mapper.readValue(config, List.class);
} catch (Exception e) {
logger.error("config to OftenQuestionAo fail , error {} ", e);
return ClientResponseFactory.create(ErrorEnum.DATA_PARSE_ERROR);
}
return ClientResponseFactory.create(result);
}
// 后续补上
二、
本文介绍如何在Spring框架中读取JSON资源文件,并将其转换为Java对象数组。通过使用Resource目录下的/data/oftenQuestion.json文件为例,详细展示了利用InputStream和ObjectMapper实现文件读取与转换的过程。
9715

被折叠的 条评论
为什么被折叠?



