Firebase in Spring boot gives error during initalization

自古美人都是妖i 提交于 2019-12-04 05:24:54

问题


I'm trying to set up Firebase in Spring Boot app. I'm following the code snippets given in the documentation here. This is how my pom looks:

<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-admin</artifactId>
    <version>5.2.0</version>
</dependency>

The code that I run to initialise firebase:

@PostConstruct
    public void init() {
        InputStream serviceAccount = FirebaseConfig.class.getClassLoader().getResourceAsStream(configPath);

        FirebaseOptions options = null;
        try {
            options = new FirebaseOptions.Builder()
                    .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
                    .setDatabaseUrl(databaseUrl)
                    .build();
        } catch (IOException e) {
            e.printStackTrace();
        }
        FirebaseApp.initializeApp(options);

    }

On startup FirebaseApp.initializeApp is throwing below error:

[ERROR] RunLoop: Uncaught exception in Firebase Database runloop (5.2.0). Please report to firebase-database-client@google.com java.lang.NoSuchMethodError: org.json.JSONStringer.object()Lorg/json/JSONWriter; at com.google.firebase.database.util.JsonMapper.serializeJsonValue(JsonMapper.java:72) at com.google.firebase.database.util.JsonMapper.serializeJsonValue(JsonMapper.java:61) at com.google.firebase.database.util.JsonMapper.serializeJson(JsonMapper.java:41)

I have tried to include org.json but with no luck.


回答1:


Not sure if you found the answer Raj

I was about to get rid of this error by excluding a dependency pulled in by Spring Boot's configuration processor and (though I did not see this error anymore after excluding just the first) also exclude from Spring boot starter test (if used):

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <version>1.5.8.RELEASE</version>
        <scope>compile</scope>
        <exclusions>
          <exclusion> 
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android.json</artifactId>
          </exclusion>
        </exclusions> 
      </dependency>
    ...
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <version>1.5.8.RELEASE</version>
        <scope>test</scope>
        <exclusions>
          <exclusion> 
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android.json</artifactId>
          </exclusion>
        </exclusions> 
      </dependency>
    ...
  </dependencies>
</project>

Please note: I haven not tested this exact pom snippet because I am using gradle instead of maven, but this should be right.

How I found this

Reading your question and other answers, I began looking into potential issue with class JSONStringer in package 'org.json'. So I was thinking a version conflict of a dependency that depended on 'org.json'

Running ./gradlew dependencyInsight --dependency org.json, I received:

org.json:json:20160810 -> 20140107
+--- com.google.cloud:google-cloud-core:1.7.0
|    +--- com.google.cloud:google-cloud-storage:1.7.0
|    |    \--- com.google.firebase:firebase-admin:5.5.0
|    |         \--- compile
|    +--- com.google.cloud:google-cloud-firestore:0.25.0-beta
|    |    \--- com.google.firebase:firebase-admin:5.5.0 (*)
|    +--- com.google.cloud:google-cloud-core-http:1.7.0
|    |    +--- com.google.cloud:google-cloud-storage:1.7.0 (*)
|    |    \--- com.google.cloud:google-cloud-firestore:0.25.0-beta (*)
|    \--- com.google.cloud:google-cloud-core-grpc:1.7.0
|         \--- com.google.cloud:google-cloud-firestore:0.25.0-beta (*)
\--- com.google.firebase:firebase-admin:5.5.0 (*)

(*) - dependencies omitted (listed previously)

so only the google dependencies were using this package. I suspected that the issue wasn't a version conflict in the google dependecies, so I looked for conflicts that Spring may have with the org.json pacakage.

Google search for 'Spring boot org.json' led me to a Github issue about conflicts with json library. The issue mentioned that for spring-boot-starter-test since "org.skyscreamer:jsonassert:1.4.0 is required, exclude com.vaadin.external.google:android-json:0.0.20131108.vaadin1."

From that, I ran: `./gradlew dependencyInsight --dependency 'com.vaadin.external.google' which referenced 'spring-boot-configuration-processor'.



来源:https://stackoverflow.com/questions/45651768/firebase-in-spring-boot-gives-error-during-initalization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!