使用spring社交时,无法将PagedList解析为无法解析为某种类型的类型

时间:2015-04-06 06:16:00

标签: java hibernate spring-mvc spring-social

我正在使用spring和hibernate构建一个简单的应用程序,我正在尝试使用spring social将它与facebook连接。我正在初始化我的应用程序而不使用xml文件。我从其官方网站上读到了关于spring social的内容。但这是根本没有工作。虽然我正在尝试使用代码,但却给出了错误“PagedList无法解析为某种类型” 例外

   java.lang.NoClassDefFoundError: org/springframework/social/ApiBinding
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2957)
    at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1210)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1690)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.lang.Class.getDeclaredConstructors(Unknown Source)

我的控制器类是

    package net.codejava.spring.controller;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import net.codejava.spring.dao.UserDAO;
import net.codejava.spring.model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.Post;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

    private Facebook facebook;
    @Autowired
    private UserDAO userDao;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView checkLogin() {
        ModelAndView model = new ModelAndView("LoginForm");
        return model;       
    }
    @RequestMapping(value = "/fb", method = RequestMethod.GET)
    public ModelAndView fbLogin() {
        ModelAndView model = new ModelAndView("facebook");
        return model;       
    }
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public ModelAndView processCredential(@RequestParam("username")String username,@RequestParam("password")String password) {
        String message = "Invalid credentials";
        if(!username.equals("")&&!password.equals(""))
        {
            if(username.equals(username)&&password.equals(password)){
                message = "Welcome" +username+"!!!!";
            }
        }
     return new ModelAndView("result","message",message);   
    }
    @RequestMapping("/list")
    public ModelAndView handleRequest() throws Exception {
        List<User> listUsers = userDao.list();
        ModelAndView model = new ModelAndView("UserList");
        model.addObject("userList", listUsers);
        return model;
    }

    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public ModelAndView newUser() {
        ModelAndView model = new ModelAndView("UserForm");
        model.addObject("user", new User());
        return model;       
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editUser(HttpServletRequest request) {
        int userId = Integer.parseInt(request.getParameter("id"));
        User user = userDao.get(userId);
        ModelAndView model = new ModelAndView("UserForm");
        model.addObject("user", user);
        return model;       
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ModelAndView deleteUser(HttpServletRequest request) {
        int userId = Integer.parseInt(request.getParameter("id"));
        userDao.delete(userId);
        return new ModelAndView("redirect:/");      
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView saveUser(@ModelAttribute User user) {
        userDao.saveOrUpdate(user);
        return new ModelAndView("redirect:/");
    }
    @Inject
    public HomeController(Facebook facebook) {
        this.facebook = facebook;
    }
      @RequestMapping(method=RequestMethod.GET)
        public String helloFacebook(Model model) {
            if (!facebook.isAuthorized()) {
                return "redirect:/connect/facebook";
            }

            model.addAttribute(facebook.userOperations().getUserProfile());
            PagedList<Post> homeFeed = facebook.feedOperations().getHomeFeed();
            model.addAttribute("feed", homeFeed);

            return "hello";
        }

}

我的ApplicationContectConfig类是我声明Facebook的属性文件的地方

    package net.codejava.spring.config;

import java.util.Properties;

import javax.sql.DataSource;

import net.codejava.spring.dao.UserDAO;
import net.codejava.spring.dao.UserDAOImpl;
import net.codejava.spring.model.User;

import org.apache.commons.dbcp2.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan("net.codejava.spring")
@EnableTransactionManagement
public class ApplicationContextConfig {
    @Bean(name = "viewResolver")
    public InternalResourceViewResolver getViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }


    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/testing");
        dataSource.setUsername("root");
        dataSource.setPassword("4261");

        return dataSource;
    }


    private Properties getHibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        return properties;
    }
    private Properties getFacebookProperties(){
        Properties properties = new Properties();
        properties.put("spring.social.facebook.appId", "809280815833386");
        properties.put("spring.social.facebook.appSecret","24035c462c9d1fca367ff813436b15d8");

        return properties;
    }

    @Autowired
    @Bean(name = "sessionFactory")
    public SessionFactory getSessionFactory(DataSource dataSource) {
        LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
        sessionBuilder.addProperties(getHibernateProperties());
        sessionBuilder.addAnnotatedClasses(User.class);
        return sessionBuilder.buildSessionFactory();
    }

    @Autowired
    @Bean(name = "transactionManager")
    public HibernateTransactionManager getTransactionManager(
            SessionFactory sessionFactory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager(
                sessionFactory);

        return transactionManager;
    }

    @Autowired
    @Bean(name = "userDao")
    public UserDAO getUserDao(SessionFactory sessionFactory) {
        return new UserDAOImpl(sessionFactory);
    }
}

我已将我的Facebook属性文件放在src / main / resources下 请提供解决方案

1 个答案:

答案 0 :(得分:0)

你展示的例外没有说明PagedList。但是,您提到的异常或您显示的异常都可以解释为类路径问题。根据您所说和所示,您的运行时类路径似乎缺少Spring Social库。我无法解释为什么会这样,因为我不知道你是如何构建或运行应用程序的。如果有一个我可以看的GitHub回购,那可能会有用。否则我只是在猜测。