如何在没有spring的情况下使用hibernate tomcat 7 postgres创建rest web服务项目
我需要创建一个带有hibernate但没有spring框架的RESTWeb服务项目 我已经创建了一个maven项目、模型、Dao和服务包如何在没有spring的情况下使用hibernate tomcat 7 postgres创建rest web服务项目,hibernate,rest,Hibernate,Rest,我需要创建一个带有hibernate但没有spring框架的RESTWeb服务项目 我已经创建了一个maven项目、模型、Dao和服务包 请您帮助我或告诉我一个教程???使用泽西HK2,您不需要使用web xml文件: 1) 创建应用程序类: @ApplicationPath("rest") public class Application extends ResourceConfig { public SapApplication() { p
请您帮助我或告诉我一个教程???使用泽西HK2,您不需要使用web xml文件: 1) 创建应用程序类:
@ApplicationPath("rest")
public class Application extends ResourceConfig {
public SapApplication() {
packages("sap.ressources", "sap.providers");
registerInstances(new SapBinder());
register(MoxyJsonFeature.class);
}
}
接下来,您将创建一个如下所示的绑定类:
public class Binder extends AbstractBinder {
@Override
protected void configure() {
bind(ADAOImpl.class).to(ADAO.class);
} // implement class to inteface use the same thing for services classes
您还需要创建一个侦听器,以便在DAO类中创建entityManager:
@WebListener
public class LocalEntityManagerFactory implements ServletContextListener {
private static EntityManagerFactory emf;
@Override
public void contextInitialized(ServletContextEvent event) {
emf = Persistence.createEntityManagerFactory("myPU");// myPu : is a name of persistence-unit in persistence xml file
}
@Override
public void contextDestroyed(ServletContextEvent event) {
if (emf != null) {
emf.close();
}
}
public static EntityManager createEntityManager() {
if (emf == null) {
throw new IllegalStateException("Context is not initialized yet.");
}
return emf.createEntityManager();
}
就这些,现在您可以创建rest服务了。也许EJB的这一点很有用,使用Mysql而不是Postgress,但是更改数据库很容易。