在Java Spring中获取资源URL路径

时间:2016-11-08 13:35:03

标签: java spring firebase path

尝试获取我在资源文件夹中的JSON文件的路径,但没有使用getResource().getPath()。我希望以尽可能强大的方式获取此URL。

public static void initializeFirebase() {
        try {
            URL url = getResource("google-services.json");
            System.out.println(url.getPath());
            FirebaseOptions options = new FirebaseOptions
                    .Builder()
                    .setServiceAccount(new FileInputStream(url.getPath()))
                    .setDatabaseUrl("https://XXXXX.firebaseio.com/")
                    .build();
            FirebaseApp.initializeApp(options);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

以上功能打印: 在抛出错误之前/Users/username/Documents/Spring_Project/springbackend/build/resources/main/google-services.json

Exception in thread "main" java.lang.RuntimeException: Failed to read service account
    at com.google.firebase.FirebaseOptions$Builder.setServiceAccount(FirebaseOptions.java:82)
    at live.cibc.labs.springbackend.SpringbackendApplication.initializeFirebase(SpringbackendApplication.java:188)
    at live.cibc.labs.springbackend.SpringbackendApplication.main(SpringbackendApplication.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.json.JSONException: JSONObject["project_id"] not found.

显然它无法找到我的project_id,因为我的google-services.json位于src/main/resources/google-services.json下。

我应该如何以尽可能强大的方式获取路径?

谷歌services.json

{
  "project_info": {
    "project_number": "XXXXXXXX",
    "firebase_url": "https://XXXXXX.firebaseio.com",
    "project_id": "XXXX-XXX",
    "storage_bucket": "XXXXXXXX.appspot.com"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:XXXXXXXXX:android:XXXXXXX",
        "android_client_info": {
          "package_name": "my.spring.project"
        }
      },
      "oauth_client": [
        {
          "client_id": "XXXXX-XXXXX.apps.googleusercontent.com",
          "client_type": 3
        }
      ],
      "api_key": [
        {
          "current_key": "XXXXXX"
        }
      ],
      "services": {
        "analytics_service": {
          "status": 1
        },
        "appinvite_service": {
          "status": 1,
          "other_platform_oauth_client": []
        },
        "ads_service": {
          "status": 2
        }
      }
    }
  ],
  "configuration_version": "1"
}

2 个答案:

答案 0 :(得分:2)

答案最终成为我真正愚蠢的事情!在firebase控制台中,您可以下载2个JSON文件。

  • google-services.json适用于客户
  • service-account-credentials.json用于服务器管理SDK(我应该使用的那个)

不要混淆两个!

答案 1 :(得分:0)

如果我理解你使用Spring,最好的方法是使用Springs ClassPathResource

public static void initializeFirebase() {    
  try {
      Resource googleServicesResource = new ClassPathResource("google-services.json");
      FirebaseOptions options = new FirebaseOptions
              .Builder()
              .setServiceAccount(googleServicesResource.getInputStream())
              .setDatabaseUrl("https://XXXXX.firebaseio.com/")
              .build();
      FirebaseApp.initializeApp(options);
  } catch (IOException e) {
      e.printStackTrace();
  }
}