Spring RESTful和Hibernate

时间:2014-06-17 11:13:01

标签: spring hibernate rest tomcat

我在postgres中有一个非常简单的数据库,我已经使用了hibernate来连接" connect"它。一切正常,我用hibernate测试数据库,到目前为止没有问题。

这是我的DAO

@Repository("clientsBasicDao")
@Transactional
public class ClientsBasicDaoImpl implements ClientsBasicDao {


private Log log = LogFactory.getLog(ClientsBasicDaoImpl.class);
private SessionFactory sessionFactory;  
private Session session;


public SessionFactory getSessionFactory() {
    return sessionFactory;
}


@Resource(name="sessionFactory")
public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
    session = sessionFactory.openSession();
}

@SuppressWarnings("unchecked")
@Transactional(readOnly=true)
public List<ClientsBasic> findAllClients() throws HibernateException{
    return session.createQuery("from ClientsBasic").list();
}

public ClientsBasic findClientById(int id) throws HibernateException {
    return (ClientsBasic) session.
            getNamedQuery("ClientsBasic.findById").setParameter("id", id).uniqueResult();
}

public ClientsBasic findClientByEmail(String email) throws HibernateException{
    return (ClientsBasic) session.
            getNamedQuery("ClientsBasic.findByEmail").setParameter("email", email).uniqueResult();
}

@SuppressWarnings("unchecked")
public List<ClientsBasic> findDirectClients() throws HibernateException{
    return session.getNamedQuery("ClientsBasic.findDirectClients").list();
}

@SuppressWarnings("unchecked")
public List<ClientsBasic> findIndirectClients() throws HibernateException{
    return session.getNamedQuery("ClientsBasic.findIndirectClients").list();
}

public ClientsBasic save(ClientsBasic client) throws HibernateException {
    Transaction tx = null;
    tx = session.beginTransaction();
    session.saveOrUpdate(client);
    tx.commit();
    log.info("Client saved with id: " + client.getClientId());

    return client;
}
public void delete(ClientsBasic client) throws HibernateException  {
    Transaction tx = null;

    tx = session.beginTransaction();
    Set<Resources> res = client.getClientResources();
    if(res.size() > 0){ //there are client access resources for this client
        Iterator<Resources> it = res.iterator();
        while(it.hasNext()){
            Resources resource = it.next();
            resource.getClientsBasics().remove(client);
        }
    }
    session.delete(client);
    tx.commit();
    log.info("Client deleted with id: " + client.getClientId());    

}

}

现在,我正在尝试学习宁静的Web服务,所以在一些教程之后我尝试了自己的实现。它起作用,除非我尝试使用连接到数据库的方法。

@RestController
public class GreetingController {

private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();


//http://localhost:8080/TestProject/greeting  
//          or
//http://localhost:8080/TestProject/greeting?name=stackoverflow
@RequestMapping("/greeting")
public @ResponseBody String greeting(
        @RequestParam(value="name", required=false, defaultValue="World") String name) {
    return new Greeting(counter.incrementAndGet(),
                        String.format(template, name)).toString();
}


//http://localhost:8080/TestProject/testing
@RequestMapping("/testing")
public @ResponseBody String home(){
    return "Welcome, the server is now up and running!";
}


//http://localhost:8080/TestProject/client?id=1
@RequestMapping("/client") 
public @ResponseBody String client(     //FAILS HERE
        @RequestParam(value="id", required=true) String id){

    ClientServiceBackend cb = new ClientServiceBackend();
    return cb.findClient(Integer.parseInt(id));
}

}

这是错误

HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/HibernateException 

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/HibernateException
org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1284)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:965)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>test</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>


<servlet>
    <servlet-name>rest</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

和调度员:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <context:component-scan base-package="com.project.rest" />
    <mvc:annotation-driven />
  </beans>
像我说的那样,我正在努力学习休息,这是我春天的第二周,所以这对我来说都是新的,但我希望这可以工作,因为数据库工作正常!

注意:我正在Tomcat v7上部署它

有人可以请一下吗?

谢谢: - )

修改 我将hibernate jar添加到tomcat类路径,现在错误是 匹配的通配符是严格的,但是找不到元素&t; tx:annotation-driven&#39;

的声明。

0 个答案:

没有答案