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;

}

}


版权声明:本文为weixin_39990138原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。