繁体   English   中英

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

[英]Read Facebook Post-related Object with 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)

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

我想你只是希望能够将对象作为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()发出请求并自行处理绑定。 这显然对你来说还有很多工作要做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM