Springboot外部jsonファイル構成クラスの読み出し

4863 ワード

説明
システムを実行するときには、デフォルトのユーザー、さまざまなデータの列挙など、一般的なデータをロードすることは避けられません.現在流行している方法は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
", "").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
", "").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; } }

詳細については、springbootテクノロジーの実践概要に注目してください.