我是Spring Framework的初学者,想试试Spring Social来制作一个简单的Web应用程序,它从Facebook中检索数据。为此我遵循Spring Socials官方“入门指南”,名为“访问Facebook数据”。Spring Social 3.0.0.M1中的org.springframework.social.connect.ConnectionRepository类发生了什么?
我遇到的第一个问题是,Spring社交版本2.0.3.RELEASE,它似乎是Spring Social的最新正式版本,不支持facebook API的2.8版本(因此给了我以下错误:“(#12)生物字段已被弃用,版本v2.8及更高版本”)。正如我昨天在developers.facebook.com上创建了Facebook应用程序,似乎我无法访问以前的API版本。
我搜索谷歌的解决方案,并发现版本3.0.0.M1似乎是可用的Maven存储库,这是应该解决这个问题。但是当我在我的.pom文件中更改配置以使用此版本时,编译器无法再找到类ConnectionRepository。其实整个包org.springframework.social.connect似乎缺少。
的代码,这是我从导向复制(https://spring.io/guides/gs/accessing-facebook/)看起来followig:
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” ClassLoader [[email protected]]中的类[hello.HelloController]失败;嵌套的例外是java.lang.NoClassDefFoundError:组织/ springframework的/社会/ 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";
}
}
非常具有启发性。我发现3.0.0.M1是在春节里程碑库,所以不得不将它添加到我的POM第一: '<库> 春里程碑库 http://repo.spring.io /里程碑/ 存储库> –
acdhirr