Amine MEHARZI
Amine MEHARZI

Reputation: 33

how create a rest web service project with hibernate tomcat 7 postgres without spring

i need create a rest web service project with hibernate but without spring framework.

I have create a maven project, model, Dao and services packages

please can you help me or tell me a tutorial ??

Upvotes: 0

Views: 454

Answers (1)

Amine MEHARZI
Amine MEHARZI

Reputation: 33

Using HK2 of jersey, you d'ont need use a web xml file:

1) create a application class :

@ApplicationPath("rest")

    public class Application extends ResourceConfig {

        public SapApplication() {
            packages("sap.ressources", "sap.providers");
            registerInstances(new SapBinder());
            register(MoxyJsonFeature.class);
        }

}

next you will create a Bind class like this :

    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

You need also to create a listener to can create an entityManager in DAO Classes:

@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();
        }

that's all, now you can create your rest service.

Upvotes: 0

Related Questions