raj
raj

Reputation: 561

Firebase in Spring boot gives error during initalization

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 [email protected] 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.

Upvotes: 1

Views: 2816

Answers (1)

Julio C Villalta III
Julio C Villalta III

Reputation: 134

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'.

Upvotes: 1

Related Questions