如何在运行 grizzly 的 java se 上启用 Web 服务(jaxrs/jersey)中的 CDI 注入?

2022-01-21 00:00:00 java cdi jax-rs jersey grizzly

如何允许 CDI 将资源注入 RESTful Web 服务资源?我使用weld 2 (cdi)、jersey (jaxrs) 和grizzly (web server) 在标准java 上运行.这是我的简单网络资源:

How do I allow CDI injection of resources into restful web service resources? I am running on standard java using weld 2 (cdi), jersey (jaxrs), and grizzly (web server). Here is my simple web resource:

import training.student.StudentRepository;
import javax.inject.Inject;
import javax.ws.rs.*;

@Path("student")
public class StudentWebResource {
  @Inject
  private StudentRepository studentRepository;  

  @GET
  @Path("count")
  @Produces(MediaType.TEXT_PLAIN)
  public Integer getCount() {
    return studentRepository.studentCount();
  }
}

下面是我如何焊接启动我的简单网络服务器:

And here is how I've got weld starting my simple web server:

public class Main {
  public static void main(String[] args) throws Exception {
    startCdiApplication();
  }

  public static void startCdiApplication() throws Exception {
    Weld weld = new Weld();
    try {
      WeldContainer container = weld.initialize();
      Application application = container.instance().select(WebServer.class).get();
      application.run();
    } 
    finally {
      weld.shutdown();
    }
  }
}

我怀疑需要修改代码以通知泽西使用焊接进行 CDI 注入分辨率:

And the code that I suspect will need to be modified to inform jersey to use weld for CDI inject resolution:

...
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;

public class WebServer implements Application {

  /*
   * startup the grizzly http server to make available the restful web services
   */
  private void startWebServer() throws IOException, InterruptedException {
    final ResourceConfig resourceConfig = new ResourceConfig().packages("training.webservice").register(new JacksonFeature());
    final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(getBaseUri(), resourceConfig);
    server.start();
    Thread.currentThread().join();
  }

  ...

  @Override
  public void run() throws IOException, InterruptedException {
    startWebServer();
  }
}

推荐答案

看到这个stackoverflow post,我实现了以下解决方案.不确定这是否是最好的路线,但它确实有效.

After seeing this stackoverflow post, I implemented the following solution. Not sure if it is the best route to take, but it worked.

我创建了一个hk2 Binder并注册了Binder:

I created an hk2 Binder and registered the Binder:

public class WebServiceBinder extends AbstractBinder {

  @Override
  protected void configure() {
    BeanManager bm = getBeanManager();
    bind(getBean(bm, StudentRepository.class))
        .to(StudentRepository.class);
  }

  private BeanManager getBeanManager() {
    // is there a better way to get the bean manager?
    return new Weld().getBeanManager();
  }

  private <T> T getBean(BeanManager bm, Class<T> clazz) {
    Bean<T> bean = (Bean<T>) bm.getBeans(clazz).iterator().next();
    CreationalContext<T> ctx = bm.createCreationalContext(bean);
    return (T) bm.getReference(bean, clazz, ctx); 
  }
}

然后将上面的 ResourceConfig 实例修改为:

Then modified the ResourceConfig instantiation from above to:

final ResourceConfig resourceConfig = new ResourceConfig()
    .packages("training.webservice")
    .register(new JacksonFeature())
    .register(new WebServiceBinder());

相关文章