我正在尝试将 twitter4j 状态对象保存到 Mongodb。我有以下代码:
public void saveTweets(Status status) throws Exception {
BasicDBObject tweet = new BasicDBObject();
tweet.put("tweet_id", status.getId());
tweet.put("user", status.getUser());
tweet.put("text", status.getText());
tweet.put("location", status.getGeoLocation());
tweet.put("place", status.getPlace());
tweet.put("created_at", status.getCreatedAt());
tweet.put("contributors", status.getContributors());
tweet.put("hashtag_entities", status.getHashtagEntities());
tweet.put("media_entities", status.getMediaEntities());
tweet.put("user_mention_entities", status.getUserMentionEntities());
tweet.put("url_entities", status.getURLEntities());
tweet.put("source", status.getSource());
tweet.put("retweeted_status", status.getRetweetedStatus());
tweet.put("retweeted_count", status.getRetweetCount());
tweet.put("count", 0);
tweetsDAO.saveToDB(tweetsCollectionName, tweet);
}
但这引发了以下异常:
java.lang.IllegalArgumentException: can't serialize class twitter4j.internal.json.UserJSONImpl
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:270)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:174)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:120)
at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
at com.mongodb.OutMessage.putObject(OutMessage.java:289)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:239)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:204)
at com.mongodb.DBCollection.insert(DBCollection.java:76)
at com.mongodb.DBCollection.insert(DBCollection.java:60)
at com.mongodb.DBCollection.insert(DBCollection.java:105)
似乎我需要更多地进入状态对象并为返回的每个实体单独设置 BasicDBObject。但这是一项相当大的任务,因为几乎所有 status.get*** 调用都会返回另一个 twitter4j 实体,该实体具有一组字段。
有没有更好的方法来做到这一点?
谢谢。