Facebook实例未在Spring Social中创建

时间:2015-09-13 08:56:02

标签: spring-boot spring-social spring-social-facebook

我正在尝试运行一个简单的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

3 个答案:

答案 0 :(得分:0)

您缺少facebook依赖关系,请参阅下文:

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

请点击以下链接获取更多信息:

Accessing Facebook Data

答案 1 :(得分:0)

Do you have this project in a Git repository somewhere that I could try without having to recreate the example from the blog entry you pulled it from?

I also notice that your Maven dependencies are reference Spring Boot starters for Spring Social Facebook, but that you are explicitly configuring the connection factory locator (and nothing else). Consequently, if you are using Spring Boot, then your explicit configuration is probably disabled auto-configuration provided by Spring Boot. Delete SocialConfig and see what happens.

答案 2 :(得分:0)

您可以更改解决方法的代码以使其正常工作。在这个时候,Facebook没有连接或注入,因此它正在获得NPE:

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

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