springboot传入json和文件_springboot 读取外部json文件配置类

说明

我们在运行一个系统时,不可避免的要加载一些常用的数据,如默认用户,各种数据枚举等。

现在流行的方式为配置一个json文件,此json文件可以放在外部系统读取,也可以放入src/main/resources下,读取的顺序可以和 application.yml 文件一样。加载优先级:外部同级目录 > 外部config目录 > 内部 resources下同级目录 > 内部 resources下config目录。

其实是一段函数控制的:

private static File getResFile(String filename) throws FileNotFoundException {

File file = new File(filename);

if (!file.exists()) { // 如果同级目录没有,则去config下面找

log.debug("不在同级目录,进入config目录查找");

file = new File("config/"+filename);

}

Resource resource = new FileSystemResource(file);

if (!resource.exists()) { //config目录下还是找不到,那就直接用classpath下的

log.debug("不在config目录,进入classpath目录查找");

file = ResourceUtils.getFile("classpath:"+filename);

}

return file;

}

那么具体的实现json文件的解析类,就是:

外部依赖

com.alibaba

fastjson

1.2.51

JSONHelper.java

@Slf4j

public class JSONHelper {

/* public static void main(String[] args) {

// String s =ResolveJsonFileToString("node.json");

// System.out.println("sss="+s);

// Map map = (Map) ResolveJsonFileToObject("node.json");

// System.out.println("map="+map.get("res"));

}

*/

/**

* 将文件流转为json对象,文件存放路径与配置文件路径规范一致

* @param

* @return

* @throws

*/

public static Object ResolveJsonFileToObject(String filename){

String str= ResolveJsonFileToString(filename);

JSONObject jo = JSONObject.parseObject(str);

return jo;

}

/**

* 通过文件名获取获取json格式字符串,

* @param filename 文件存放路径与配置文件路径规范一致

* @return ResolveJsonFileToString

* @throws

*/

public static String ResolveJsonFileToString(String filename){

BufferedReader br = null;

String result = null;

try {

// br = new BufferedReader(new InputStreamReader(getInputStream(path)));

br = new BufferedReader(new InputStreamReader(getResFileStream(filename),"UTF-8"));

StringBuffer message=new StringBuffer();

String line = null;

while((line = br.readLine()) != null) {

message.append(line);

}

if (br != null) {

br.close();

}

String defaultString=message.toString();

result=defaultString.replace("\r\n", "").replaceAll(" +", "");

log.info("result={}",result);

} catch (IOException e) {

try {

ClassLoader classloader = Thread.currentThread().getContextClassLoader();

InputStream in = classloader.getResourceAsStream(filename);

br = new BufferedReader(new InputStreamReader(in,"UTF-8"));

StringBuffer message=new StringBuffer();

String line = null;

while((line = br.readLine()) != null) {

message.append(line);

}

if (br != null) {

br.close();

}

if (in != null){

in.close();

}

String defaultString=message.toString();

result=defaultString.replace("\r\n", "").replaceAll(" +", "");

log.debug("for jar result={}",result);

}catch (Exception e1){

e1.printStackTrace();

}

}

return result;

}

private static File getResFile(String filename) throws FileNotFoundException {

File file = new File(filename);

if (!file.exists()) { // 如果同级目录没有,则去config下面找

log.debug("不在同级目录,进入config目录查找");

file = new File("config/"+filename);

}

Resource resource = new FileSystemResource(file);

if (!resource.exists()) { //config目录下还是找不到,那就直接用classpath下的

log.debug("不在config目录,进入classpath目录查找");

file = ResourceUtils.getFile("classpath:"+filename);

}

return file;

}

/**

* 通过文件名获取classpath路径下的文件流

* @param

* @return

* @throws

*/

private static FileInputStream getResFileStream(String filename) throws FileNotFoundException {

FileInputStream fin = null;

File file = getResFile(filename);

log.info("getResFile path={}",file);

fin = new FileInputStream(file);

return fin;

}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值