我是 Spring 框架的初学者,想尝试使用 Spring Social 来制作一个从 Facebook 检索数据的简单 Web 应用程序。为此我关注了 Spring Socials 官方“入门指南“叫”访问 Facebook 数据".
我遇到的第一个问题是Spring Social版本2.0.3.发布,这似乎是 Spring Social 的最新官方版本,不支持 facebook API 2.8 版本(因此出现以下错误:“(#12) v2.8 及更高版本已弃用 bio 字段“)。由于我昨天在developers.facebook.com上创建了Facebook应用程序,所以我似乎无法访问以前的API版本。
我用谷歌搜索解决方案并找到了该版本3.0.0.M1似乎可以在maven存储库中找到,这应该可以解决这个问题。但是当我更改 .pom 文件中的配置以使用此版本时,编译器找不到该类连接存储库不再了。其实整个包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";
}
}