【发布时间】:2017-02-10 21:05:04
【问题描述】:
我是 Spring 框架的初学者,想尝试使用 Spring Social 来制作一个简单的 Web 应用程序,该应用程序可以从 Facebook 检索数据。为此,我遵循了名为“访问 Facebook 数据”的 Spring Socials 官方“入门指南”。
我遇到的第一个问题是Spring Social版本2.0.3.RELEASE,好像是Spring Social的最新正式版,不支持facebook API 2.8版本(和因此给了我以下错误:“(#12) bio field is deprecated for version v2.8 and higher”)。由于我昨天在developers.facebook.com 上创建了Facebook 应用程序,因此我似乎无法访问以前的API 版本。
我用 google 搜索了一个解决方案,发现版本 3.0.0.M1 似乎在 maven 存储库中可用,应该可以解决这个问题。但是,当我将 .pom 文件中的配置更改为使用此版本时,编译器再也找不到类 ConnectionRepository 了。实际上整个包 org.springframework.social.connect 似乎不见了。
我从指南 (https://spring.io/guides/gs/accessing-facebook/) 复制的代码如下所示:
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.PagedList;
import org.springframework.social.facebook.api.Post;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/")
public class HelloController {
private Facebook facebook;
private ConnectionRepository connectionRepository;
public HelloController(Facebook facebook, ConnectionRepository connectionRepository) {
this.facebook = facebook;
this.connectionRepository = connectionRepository;
}
@GetMapping
public String helloFacebook(Model model) {
if (connectionRepository.findPrimaryConnection(Facebook.class) == null) {
return "redirect:/connect/facebook";
}
model.addAttribute("facebookProfile", facebook.userOperations().getUserProfile());
PagedList<Post> feed = facebook.feedOperations().getFeed();
model.addAttribute("feed", feed);
return "hello";
}
}
是否已弃用 ConnectionRepository 并已将其删除?如果是这种情况,我应该改用其他东西吗?还是我错过了什么?
仅删除对 ConnectionRepository 的所有引用会在启动应用程序时给我以下错误:
org.springframework.beans.factory.BeanCreationException:创建名称为“helloController”的 bean 时出错:解析来自 ClassLoader [sun.misc.Launcher$AppClassLoader@73d16e93] 的 bean 类 [hello.HelloController] 上声明的构造函数失败的;嵌套异常是 java.lang.NoClassDefFoundError: org/springframework/social/ApiBinding
在这种情况下,代码如下所示:
package hello;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.PagedList;
import org.springframework.social.facebook.api.Post;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/")
public class HelloController {
private Facebook facebook;
public HelloController(Facebook facebook) {
this.facebook = facebook;
}
@GetMapping
public String helloFacebook(Model model) {
model.addAttribute("facebookProfile", facebook.userOperations().getUserProfile());
PagedList<Post> feed = facebook.feedOperations().getFeed();
model.addAttribute("feed", feed);
return "hello";
}
}
【问题讨论】:
标签: spring facebook-graph-api spring-social spring-social-facebook facebook-graph-api-v2.8