Jersey - Is there a way to instantiate a Per-Request Resource with parameters?

假如想象 提交于 2019-12-07 10:42:05

问题


Suppose I have this class:

@Path("/test")
public class TestResource{

   private TestService testService;

   public TestResource(TestService testService){
      this.testService = testService;
   }

   @GET
   public String getMessage(){
      return testService.getMessage();
   }


}

Then, I want to register it with Jersey. One would do:

 TestService testService = new TestServiceImpl();
 resourceConfig.register(new TestResource(testService));

But the problem is that approach makes a single instance of TestResource. I want to make an instance per request. So, Jersey has a way of doing this:

 TestService = new TestServiceImpl();
 resourceConfig.register(TestResource.class);

That is great! But it doesn't let me to pass instantiation parameters to its constructor.

On top of this, I'm using DropWizard which also doesn't let me access all the methods from the ResourceConfig (but I checked its documentation and didn't find any method that let me pass parameters)

So I was wondering if there is a way of achieving this.

Thanks!

PS: I know I can get away with this using Spring or Guice (or my own instance provider) and injecting my classes, but I don't want to do that (of course I'll do it if it's the only way)


回答1:


As @peeskillet has mentioned, jersey 2.x is tightly coupled with HK2 and it seems that it's the only way to pass a service to it.

@Singleton
@Path("/test")
public class TestResource {

   private TestService testService;

   @Inject
   public TestResource(TestService testService){
      this.testService = testService;
   }

   @GET
   public String getMessage(){
      return testService.getMessage();
   }
}

And this is how you register:

environment.jersey().register(new AbstractBinder(){
   @Override
   protected void configure() {
      bind(TestServiceImpl.class).to(TestService.class);
      // or
      bind(new TestServiceImpl()).to(TestService.class);
   });
environment.jersey().register(TestResource.class);


来源:https://stackoverflow.com/questions/30215676/jersey-is-there-a-way-to-instantiate-a-per-request-resource-with-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!