【问题标题】:Spring Social - Facebook.isAuthorized throws NullPointerExceptionSpring Social - Facebook.isAuthorized 抛出 NullPointerException
【发布时间】:2015-07-11 22:49:28
【问题描述】:

我正在为类似于社交中心的课程制作 Web 应用程序,我正在将 Java 与 Spring 以及 Spring 社交模块一起使用。该应用程序的重点是有一个地方,您可以在其中浏览您的 facebook 和 twitter 帐户的提要。因此,目前该应用程序允许创建帐户、登录、退出和浏览提要。我本身并没有使用 spring social 进行登录,因为该应用程序使用特定于该应用程序的帐户,但在登录后,用户可以选择将他/她的 facebook 或 twitter 帐户链接到该应用程序。 该应用程序成功连接到 facebook 和 twitter,并毫无问题地检索提要。我的项目基于春季社交快速入门,可以在 github Spring social quickstart project 中找到。 问题本身是,当用户在没有将 twitter 或 facebook 帐户链接到应用程序的情况下进入提要页面时,它会引发 NullPointerException。这是发生异常的方法的代码

@RequestMapping(value={"/", "feed"}, method = RequestMethod.GET)
public String llenarFeed(Principal principal, Model model){
    boolean emptyFeed = true;
    if(facebook != null){
            if( facebook.isAuthorized()){
                model.addAttribute("fbFeed", facebook.feedOperations().getHomeFeed());
                model.addAttribute("perfil", facebook.userOperations().getUserProfile());
                emptyFeed = false;
            }
    }
    if (twitter  != null) {
        model.addAttribute("timeline", twitter.timelineOperations().getHomeTimeline());
        emptyFeed = false;
    }
    model.addAttribute("usuario", usuarioService.loadUsuarioByUsername(principal.getName()));
    return "feed";
}

问题出现在这一行 if( facebook.isAuthorized()) 所以我知道facebook不是null,但是调用isAuthorized会导致NullPointerException,问题是如何解决呢?

【问题讨论】:

  • 你是如何实例化 Facebook 对象的?即私人脸书脸书; @Inject public llenarFeed(Facebook facebook) { this.facebook = facebook; }
  • @smoggers 在我的该方法所属的控制器中,我有一个用@Inject 注释的属性称为facebook,Bean 在一个用@Configuration 注释的SocialConfig 类中初始化,并实现SocialConfigurer 和bean 的方法是@Bean @Scope(value="request", proxyMode=ScopedProxyMode.INTERFACES) public Facebook facebook(ConnectionRepository repository) { Connection<Facebook> connection = repository.findPrimaryConnection(Facebook.class); return connection != null ? connection.getApi() : null; }
  • 就像我说的那样,我连接到 facebook 或 twitter 并检索 home feed 没有问题,问题是如何知道用户是否没有与他们每个人连接,因为 isAuthorized() 会抛出 NullPointerException脸书和推特。
  • 你用的是什么版本?
  • @The1Fitz 对于 spring social core、web 和 config 1.1.2,spring social facebook 是 2.0.1 版,对于其他 spring 包我使用 4.0.3 版

标签: spring facebook twitter spring-social


【解决方案1】:

我用 try-catch 解决了。当发现错误时,它会重定向到连接页面。

@RequestMapping(value={"/", "feed"}, method = RequestMethod.GET)
public String llenarFeed(Principal principal, Model model){
    boolean emptyFeed = true;
    try{
       if(facebook != null){
            if( facebook.isAuthorized()){
                model.addAttribute("fbFeed", facebook.feedOperations().getHomeFeed());
                model.addAttribute("perfil", facebook.userOperations().getUserProfile());
                emptyFeed = false;
            }
      }
  }
  catch{
     return "redirect:/connect/facebook";
}
    if (twitter  != null) {
        model.addAttribute("timeline", twitter.timelineOperations().getHomeTimeline());
        emptyFeed = false;
    }
    model.addAttribute("usuario", usuarioService.loadUsuarioByUsername(principal.getName()));
    return "feed";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2017-06-06
    • 2017-08-11
    • 2013-09-07
    • 2013-06-18
    相关资源
    最近更新 更多