使用Spring Social阅读Facebook Post-related对象

时间:2013-06-03 13:47:54

标签: java facebook api spring-social

我使用带有Spring Social Facebook

的FQL阅读了所有喜欢的内容

这是方法:

public List<LikeObject>  startF(String token){
    Facebook facebook = new FacebookTemplate(token);
    FacebookProfile profile = facebook.userOperations().getUserProfile();
    //System.out.println(facebook.isAuthorized());
    System.out.println("Authenticcated user: "+profile.getFirstName()+" "+profile.getLastName());
    PagedList<String> friendListIds =  facebook.friendOperations().getFriendIds();

    List<LikeObject> resultsLikes = facebook.fqlOperations().query("SELECT object_id, object_id_cursor,object_type, post_id, post_id_cursor, user_id "+
            "FROM like "+
            "WHERE user_id =me() ",  new FqlResultMapper<LikeObject>(){
        public LikeObject mapObject(FqlResult result) {
            LikeObject like = new LikeObject();
            like.object_id = result.getString("object_id");
            like.object_id_cursor =  result.getString("object_id_cursor");
            like.object_type = result.getString("object_type");
            like.post_id = result.getString("post_id"); 
            like.post_id_cursor = result.getString("post_id_cursor");
            like.user_id = result.getString("user_id");
            return like;
        }
    });
    return resultsLikes;
}       

结果如下:

  

LikeObject [object_id = 578416 ..,object_id_cursor = null,   object_type = status,post_id =,post_id_cursor = null,user_id = 10217 ..]

然后我想解析like.object_id并将其转换为java对象。但是我不知道如何使用spring social facebook来做到这一点。

我试过facebook.fetchObject(like.object_id, PostType.STATUS)。但这似乎是一种错误的方式。

有没有办法解析spring社交中的“object_id”而不解析GET查询的原始JSON响应?

我试过这个:

LinkPost post = facebook.fetchObject(obj_id, LinkPost.class);

我相信obj_id有类型链接,我已经检查了

并导致以下异常:

Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'postType' that is to contain type id  (for class org.springframework.social.facebook.api.LinkPost)
 at [Source: java.io.ByteArrayInputStream@6ca79a6a; line: 1, column: 11114]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'postType' that is to contain type id  (for class org.springframework.social.facebook.api.LinkPost)
 at [Source: java.io.ByteArrayInputStream@6ca79a6a; line: 1, column: 11114]
    at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:171)
    at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:163)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:94)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:491)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:460)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:228)
    at org.springframework.social.facebook.api.impl.FacebookTemplate.fetchObject(FacebookTemplate.java:202)
    at com.repost.facebook.FbConnection.getLikedPosts(FbConnection.java:58)
    at com.repost.facebook.MainClass.main(MainClass.java:15)

1 个答案:

答案 0 :(得分:2)

我仍然不清楚“解析”是什么意思。但是,无论如何,让我试着回答这个问题......

我认为您只是希望能够将对象作为Post(或LinkPost)对象获取......是吗?如果是这样,那么有一些特殊的魔法让我不得不在API中加入,以便能够将帖子类型多态反序列化转换为特定类型的Post(例如,LinkPost,StatusPost,等等)。

您可以在https://github.com/SpringSource/spring-social-facebook/blob/master/spring-social-facebook/src/main/java/org/springframework/social/facebook/api/impl/FeedTemplate.java中看到设置。在deserializePost()方法中,您会看到我读取了“type”属性并将其复制到新的“postType”字段中。其中一个字段用于填充Post的type属性,另一个用于确定应创建Post的哪个特定子类。解决我与杰克逊1.9.x之间的问题是一个黑客攻击......我认为杰克逊2会让那个黑客变得不必要,但我还没有尝试过。

因此,我只看到一种实用的方法:不要自己做所有的工作。而是使用facebook.feedOperations()。getPost(objectId)。它知道如何在封面下做魔术给你一个Post对象。从那里,如果您知道Post的特定类型,您可以将其转换为(如果)LinkPost,StatusPost等。

我看到的唯一其他选项是进入更低级别并通过facebook.restOperations()发出请求并自行处理绑定。显然,你的工作要多得多。