引起:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段" -Jzp5XCUx78BX5D7BvkU "
我的POJO课程
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
public class ChatData {
String messageId;
String userName;
String currentDate;
String chatType;
String messageType;
String currentLocation;
public ChatData() {
}
public ChatData(String messageId, String userName, String currentDate, String chatType, String messageType, String currentLocation) {
this.messageId = messageId;
this.userName = userName;
this.currentDate = currentDate;
this.chatType = chatType;
this.messageType = messageType;
this.currentLocation = currentLocation;
}
public String getMessageId() {
return messageId;
}
public String getUserName() {
return userName;
}
public String getCurrentDate() {
return currentDate;
}
public String getChatType() {
return chatType;
}
public String getMessageType() {
return messageType;
}
public String getCurrentLocation() {
return currentLocation;
}
}
消息接收方
public static final String CHAT_TYPE_FRIEND = "friend";
和currentChannel = 6ca5c08c-9dd2-4f8f-8475-34a427c2354f
// User node listener were all the user private messages arriving
private void listenToUserNode() {
try {
Firebase mFirebaseRef = new Firebase(Constants.FIRE_BASE_URL).child(Constants.CHAT_TYPE_FRIEND + File.separator + currentChannel);
mFirebaseRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
String key = dataSnapshot.getKey();
ChatData newMessage = dataSnapshot.getValue(ChatData.class);
handleReceivedMessage(currentChannel,newMessage);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
从firebase onChild接收数据时遇到错误,因为我的pojo与数据匹配但由于没有推送ID的字段而引发错误
答案 0 :(得分:2)
请注意,此答案基于您发布的不完整代码。如果您添加设置Firebase
引用的代码并为您的问题调用getValue()
,那将非常有用。
您似乎正在尝试将聊天消息列表读入ChatData
对象:
Firebase messagesRef = new Firebase("https://yours.firebaseio.com/messages");
messagesRef.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
ChatData data = snapshot.getValue(ChatData.class);
}
public void onCancelled(FirebaseError firebaseError) {
}
}
那不会奏效。
您有两种选择:
循环阅读每封邮件
在此代码段中,我们将使用snapshot.getChildren()
来迭代各个邮件。
messagesRef.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot child: snapshot.getChildren()) {
ChatData data = child.getValue(ChatData.class);
}
}
public void onCancelled(FirebaseError firebaseError) {
}
}
将所有讯息都写入地图
或者我们可以将所有消息读入地图:
messagesRef.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
GenericTypeIndicator<Map<String,ChatData>> chatsType = new GenericTypeIndicator<Map<String,ChatData>>() {};
Map<String,ChatData> chats = child.getValue(Map<String,ChatData>);
}
public void onCancelled(FirebaseError firebaseError) {
}
}
如果您不关心邮件的推送ID,则可以chats.values()
。但在这种情况下,您可能想要考虑为什么首先使用推送ID存储消息,而不是使用您自己的messageId
值。