Failure to write a simple POJO to Firebase (Android)

梦想的初衷 提交于 2019-12-12 04:05:56

问题


I'm trying to use Firebase Android with some simple POJO but I'm getting some exceptions even with the following sample code

final Firebase fb = getFirebaseAccess();
final Subscription subscription = new Subscription(url, System.currentTimeMillis());
Firebase subscriptionRef = fb.child("subscriptions").push();
subscriptionRef.setValue(subscription); // exception thrown here

// Subscription.java

 public class Subscription {

        private String url;
        private long subscribedAt;

        public Subscription() {
        }

        public Subscription(String url, long subscribedAt) {
            this.url = url;
            this.subscribedAt = subscribedAt;
        }

        public String getUrl() {
            return this.url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public long getSubscribedAt() {
            return this.subscribedAt;
        }

        public void setSubscribedAt(long subscribedAt) {
            this.subscribedAt = subscribedAt;
        }
    }

The following exception is thrown:

Failed to parse to snapshot com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.mypackage.Subscription and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) No serializer found for class com.myPackage.Subscription and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )

I don't understand what's failing because I kept the public default constructor and every variable has its own public getter...

Any idea ?

BTW if I replace the POJO with a String field or a map of attributes it works fine

Edit: I'm using Proguard with minify enabled. I have the following instructions:

# Firebase 2.0
# keep POJOs
-keepnames class com.myPackage.** { *; }
-keep class com.firebase.** { *; }
-keep class org.apache.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
-dontwarn org.w3c.dom.**
-dontwarn org.joda.time.**
-dontwarn org.shaded.apache.**
-dontwarn org.ietf.jgss.**

回答1:


Looks like Jackson related files are being deleted via Proguard, try with the following Proguard Configuration:

    # Proguard configuration for Jackson 2.x (fasterxml package instead of codehaus package)

        -keep class com.fasterxml.jackson.databind.ObjectMapper {
            public <methods>;
            protected <methods>;
        }
        -keep class com.fasterxml.jackson.databind.ObjectWriter {
            public ** writeValueAsString(**);
        }

        # Basic ProGuard rules for Firebase Android SDK 2.0.0+

        -keep class com.firebase.** { *; }
        -keep class org.apache.** { *; }
        -keepnames class com.fasterxml.jackson.** { *; }
        -keepnames class javax.servlet.** { *; }
        -keepnames class org.ietf.jgss.** { *; }
        -dontwarn org.apache.**
        -dontwarn org.w3c.dom.**

# Facebook

-keep class com.facebook.** {*;}
-keepattributes Signature

# Firebase UI

-dontwarn com.firebase.ui.auth.twitter.**


来源:https://stackoverflow.com/questions/35104739/failure-to-write-a-simple-pojo-to-firebase-android

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