JsonMappingException: No suitable constructor found

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

I am implementing a firebase example as given in their documentations. I am facing this error:

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.XYZ.$BlogPost]: can not instantiate from JSON object (need to add/enable type information?)

Here is my code:

    public class MainActivity extends Activity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_user);         Firebase.setAndroidContext(this);     }      @Override     protected void onStart() {         super.onStart();         // Get a reference to our posts         Firebase ref = new Firebase("https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts");         // Attach an listener to read the data at our posts reference         ref.addValueEventListener(new ValueEventListener() {             @Override             public void onDataChange(DataSnapshot snapshot) {                 System.out.println("There are " + snapshot.getChildrenCount() + " blog posts");                 for (DataSnapshot postSnapshot: snapshot.getChildren()) {                     BlogPost post = postSnapshot.getValue(BlogPost.class);                     System.out.println(post.getAuthor() + " - " + post.getTitle());                 }             }             @Override             public void onCancelled(FirebaseError firebaseError) {                 System.out.println("The read failed: " + firebaseError.getMessage());             }         });     } public class BlogPost {         private String author;         private String title;         public BlogPost() {             // empty default constructor, necessary for Firebase to be able to deserialize blog posts         }         public String getAuthor() {             return author;         }         public String getTitle() {             return title;         }     } } 

I have gone through many questions on the same thing saying to include empty constructor necessary to deserialize the JSON. I have included that but still I am not able to resolve the issue. This is the JSON I am trying to deserialize:

{      "-JRHTHaIs-jNPLXOQivY":{         "author":"gracehop",       "title":"Announcing COBOL, a New Programming Language"    },    "-JRHTHaKuITFIhnj02kE":{         "author":"alanisawesome",       "title":"The Turing Machine"    } } 

I don't know what I am missing in my code. Any help regrading this is appreciated.

回答1:

I'm guessing your BlogPost class is an inner class of your activity. In that case Java adds a hidden field to each object that refers to the containing object. And this implicit field is of course not present in your JSON data.

To solve this, you should either keep the BlogPost class in a separate file (so that it's not an inner class) or mark it as a static class (which removes the implicit field):

public static class BlogPost { 


回答2:

Add a constructor in line of:

public BlogPost(String author,String title) {     this.author=author;     this.title=title; } 

However it is also possible you need to redefine BlogPost to:

class BLogPost {   public BlogPost() { }   String postId;   List someObject;   Getters/setters } 

In which SomeObject is your current BLogPost class.



回答3:

Your JSON file is not valid. A free online JSON validator : http://jsonlint.com/



回答4:

Firebase uses -JRHTHaIs-jNPLXOQivY as the key to your object

{  author: "gracehop"  title: "Announcing COBOL, a New Programming Language" } 

You have to use this as the key to acces the object. You will have to send the correct request to the api. In this case it will be

https://docs-examples.firebaseio.com/web/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY

If you want to receive an array from Firebase you'll have to store it like this.

posts{      "1":{           author: "gracehop"           title: "Announcing COBOL, a New Programming Language"         },      "2":{          author: "alanisawesome"          title: "The Turing Machine"         } } 

Firebase has no native support for arrays. If you store an array, it really gets stored as an “object” with integers as the key names.

Check out this Firebase blog post for more information



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