0

我正在尝试运行一个简单的 Spring Social 应用程序来登录 Facebook 帐户。但是,Facebook 实例未创建,它显示 nullpointerexception。

@Controller
@RequestMapping("/fb")
public class HelloFBController {

    @Autowired
    private Facebook facebook;

    @RequestMapping(method = RequestMethod.GET)
    public String helloFacebook(Model model) {
        if (!facebook.isAuthorized()) {
            return "redirect:/connect/facebook";
        }

        model.addAttribute("feed", facebook.feedOperations().getFeed());

        return "hellofb";
    }
}

然后

@Configuration
public class SocialConfig {
    @Bean
    public ConnectionFactoryLocator connectionFactoryLocator() {
        ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();

        registry.addConnectionFactory(new FacebookConnectionFactory(
            environment.getProperty("facebook.appId"),
            environment.getProperty("facebook.appSecret")));
        return registry;
    }

    @Inject
    private Environment environment;
}

然后是application.properties文件

spring.social.facebook.appId=XXXXXXXXXXXX
spring.social.facebook.appSecret=XXXXXXXXXXXXXXXXXXX

然后 pom.xml 是

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-social-facebook</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

我是否错过了什么。当我使用 Spring Boot 运行此应用程序时,我在 Controller 中收到如下异常。我正在尝试在 localhost:8080/fb 访问应用程序

java.lang.NullPointerException: null
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
    at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at $Proxy42.isAuthorized(Unknown Source)
    at net.javabeat.spring.social.web.HelloFBController.helloFacebook(HelloFBController.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvok
4

3 回答 3

0

您可以更改解决方法的代码以使其正常工作。此时 facebook 未连接或注入,因此它正在获取 NPE:

try {
            if (!facebook.isAuthorized()) {
                return "redirect:/connect/facebook";
            }
        } catch (Exception e) {
            return "redirect:/connect/facebook";
        }

此外,如果您不使用自定义视图,您可能必须添加 spring.social.auto_connection_views=true。

于 2015-11-09T14:00:37.787 回答
0

您缺少 facebook 依赖项,请参见下文:

    <dependency>
        <groupId>org.springframework.social</groupId>
        <artifactId>spring-social-facebook</artifactId>
    </dependency>

请点击以下链接了解更多信息:

访问 Facebook 数据

于 2015-09-13T12:36:40.627 回答
0

您是否在某个 Git 存储库中有这个项目,我可以尝试而无需从您从中提取的博客条目中重新创建示例?

我还注意到您的 Maven 依赖项是 Spring Social Facebook 的参考 Spring Boot 启动器,但您正在显式配置连接工厂定位器(仅此而已)。因此,如果您使用的是 Spring Boot,那么您的显式配置可能会禁用 Spring Boot 提供的自动配置。删除 SocialConfig 看看会发生什么。

于 2015-09-14T16:00:58.190 回答