0

I'm bootstrapping a new Java restful api service using jersey and hibernate.

I'm struggling to put DI into the whole thing. Specifically I can't seem to inject hibernate stuff (EntityManagerFactory or EntityManager) and also I'd love to de-couple the rest resource (ApiResource) from the DAO object.

Here's a simplified version of my Jersey resource:

@Path("api")
public class ApiResource {

    private DBController db;

    public ApiResource() {
        this.db = DBController.getInstance("YOLO");
    }

    @GET
    @Produces({"application/json", MediaType.APPLICATION_JSON})
    @Path("/history/{id}")
    public void getTrends(@PathParam("id") String id) {
        List<Stuff> list = db.getById(id);
        return list;
    }

And then the relevant DBController (which is just a DAO) code is something like:

public class DBController {

    private static DBController instance = null;
    private static EntityManagerFactory sessionFactory;
    private static EntityManager entityManager;

    public DBController(String persistenceUnitName) {
        sessionFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
        entityManager = sessionFactory.createEntityManager();
    }

    public static DBController getInstance(String persistenceUnitName) {
        if (instance == null)
            instance = new DBController(persistenceUnitName);
        return instance;
    }

    public List<Stuff> getById(String id) {
        entityManager.getTransaction().begin();
        List<Stuff> list = entityManager.createQuery("select b from Yolo b where b.id =: ndg", Stuff.class)
                .setParameter("id", id)
                .getResultList();
        entityManager.getTransaction().commit();
        return list.stream().collect(Collectors.toList());
    }

And on my main I just do:

  ResourceConfig resourceConfig = new ResourceConfig(ApiResource.class);
  final Channel server = NettyHttpContainerProvider.createHttp2Server(BASE_URI, resourceConfig, null);

  Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
      @Override
      public void run() {
          server.close();
      }
  }));

  Thread.currentThread().join();    

If anyone has an example project using hibernate and jersey (and maybe weld?).

Note: I'm not using Spring (and don't want to). I can use weld or guice if needed. Not so sure what hk2 is for though.

mfirry
  • 3,634
  • 1
  • 26
  • 36
  • You didn't really mention container you are running on but quick scan for Jersey + Weld gave me https://github.com/eclipse-ee4j/jersey/tree/master/examples/helloworld-weld. How Weld integrates is mainly dependent on the container you are running. EE containers have it out of the box, servlets usually require extra artifact. Though the example above tells me they only have some support for Weld 2 / CDI 1.2 (whereas currently we have Weld 3 and CDI 2) – Siliarus May 06 '19 at 08:36
  • Thanks @Siliarus . Interesting note you add. I'm using jersey-container-netty-http – mfirry May 06 '19 at 08:38

0 Answers0