title: How to create a RESTful API with Jersey + Grizzly + Guice + Hibernate
body: <p>I've spent some time trying to create a RESTful API with Jersey, Grizzly, Guice and Hibernate, and I'm having a very difficult time wrapping my head around how to wire everything together. This is what I've been able to figure out on my own.</p>
  <ul>
  <li><p>I have a working DAO that implements a simple interface. I'm using Guice to inject here. I used <a href="https://stackoverflow.com/questions/57576510/how-to-prevent-reuse-of-entitymanager-with-guice-persist-and-transactional">this post</a> as a reference.</p>
  <pre class="lang-java prettyprint-override"><code> public class ProjectDAO implements ProjectDAOGeneric {
       private final Provider&lt;EntityManager&gt; entityManagerProvider;
  
       @Inject
       public ProjectDAO(Provider&lt;EntityManager&gt; entityManagerProvider) {
         this.entityManagerProvider = entityManagerProvider;
       }
  
       @Transactional
       public void insert(Project project) {
         entityManagerProvider.get().persist(project);
       }
  
   // (...)     
  </code></pre>
  </li>
  </ul>
  <hr />
  <ul>
  <li><p>I created a very simple CRUD API with Jersey. I used this <a href="https://app.pluralsight.com/library/courses/build-asynchronous-restful-services-jersey/" rel="nofollow noreferrer">course I found on PluralSight</a> for help.</p>
  <pre class="lang-java prettyprint-override"><code> @Path(&quot;/project&quot;)
   public class ProjectResource {
  
       private final ProjectDAOGeneric projectDAO;
  
       @Inject
       public ProjectResource(ProjectDAOGeneric projectDAO) {
         this.projectDAO = projectDAO;
       }
  
       @GET
       @Produces(MediaType.APPLICATION_JSON)
       public List&lt;Project&gt; getProjects() {
         return projectDAO.findAll();
       }
  
   // (...)
  </code></pre>
  </li>
  </ul>
  <hr />
  <ul>
  <li><p>From what I understand, <a href="https://javaee.github.io/hk2/guice-bridge" rel="nofollow noreferrer">I have to bridge Guice and HK2</a> so that both of them work together. I have something like this.</p>
  <pre class="lang-java prettyprint-override"><code>public class HK2toGuiceModule extends AbstractBinder {
  
    private Injector guiceInjector;
  
    public HK2toGuiceModule(Injector guiceInjector) {
      this.guiceInjector = guiceInjector;
    }
  
    @Override
    protected void configure() {
      bindFactory(new ServiceFactory&lt;ProjectService&gt;(guiceInjector, ProjectService.class)).to(ProjectService.class);
    }
  
    private static class ServiceFactory&lt;T&gt; implements Factory&lt;T&gt; {
      private final Injector guiceInjector;
      private final Class&lt;T&gt; serviceClass;
  
      public ServiceFactory(Injector guiceInjector, Class&lt;T&gt; serviceClass) {
        this.guiceInjector = guiceInjector;
        this.serviceClass = serviceClass;
      }
  
      @Override
      public T provide() {
        return guiceInjector.getInstance(serviceClass);
      }
  
      @Override
      public void dispose(T versionResource) {
      }
  
  }
  </code></pre>
  </li>
  </ul>
  <hr />
  <ul>
  <li><p>After that, I can create a simple Guice Module to bind the <code>ProjectDAO</code> interface to its implementation.</p>
  <pre class="lang-java prettyprint-override"><code>public class GuiceModule extends AbstractModule {
    @Override
    protected void configure() {
      bind(ProjectDAOGeneric.class).to(ProjectDAO.class);
    }
  }
  </code></pre>
  </li>
  </ul>
  <hr />
  <ul>
  <li>I imagine that we would then need to create a <code>ResourceConfig</code> to initialize Grizzly, also taking the Guice bridge into consideration. I have something like this.</li>
  </ul>
  <pre class="lang-java prettyprint-override"><code>@ApplicationPath(&quot;/&quot;)
  public class Config extends ResourceConfig {
  
    @Inject
    public Config(ServiceLocator serviceLocator) {
      packages(&quot;com.myproject&quot;);
      Injector injector = Guice.createInjector(new GuiceModule());
      initGuiceIntoHK2Bridge(serviceLocator, injector);
    }
  
    private void initGuiceIntoHK2Bridge(ServiceLocator serviceLocator, Injector injector) {
      GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
      GuiceIntoHK2Bridge guiceBridge = serviceLocator.getService(GuiceIntoHK2Bridge.class);
      guiceBridge.bridgeGuiceInjector(injector);
    }
  
  }
  </code></pre>
  <hr />
  <ul>
  <li>I figure that the last step would be to use that <code>ResourceConfig</code> in order to initialize the Grizzly <code>HttpServer</code>.</li>
  </ul>
  <pre class="lang-java prettyprint-override"><code>public class Main {
  
      public static final String BASE_URI = &quot;http://localhost:8080/&quot;;
  
      public static HttpServer startServer() {
          final ResourceConfig config = new ResourceConfig().packages(&quot;com.myproject&quot;);
          return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), config);
      }
  
      public static void main(String[] args) throws InterruptedException, IOException {
  
        // (...)
  </code></pre>
  <p>Now, <strong>I can't figure out how to do this.</strong> The only code I have here is the one that Grizzly provides by default. I'm not sure how to use the custom <code>ResourceConfig</code> I created.</p>
  <hr />
  <p>Most of what I found online was +5 years old, and <a href="https://stackoverflow.com/questions/2669926/practical-advice-on-using-jersey-and-guice-for-restful-service">the most relevant post on StackOverflow asking about this topic</a> is 12 years old at this point. So I figured that either Google is not being very helpful or I'm missing something. I can't seem to figure it out on my own, and I definitely haven't been able to find any up-to-date resources about it.</p>
  <p>With all this, I have two simple questions:</p>
  <ul>
  <li>Does my current approach even make sense?</li>
  <li>If it does, how can I connect everything together? What am I missing?</li>
  </ul>
  <p>Thanks a lot</p>