java - How to Disable Dependency Injection in Jersey 2? -


i'm new creating rest web services jersey 2. able few simple cases going i'm moving on "real" work.

i create own instances of resourceconfig , populate resourceconfig instances instances of rest controller returned guice:

<!-- language: lang-java --> final resourceconfig rc = new resourceconfig();  //register *instance* of rest controller rc.register(injector.getinstance(myrestcontroller.class)); 

note since these rest controller instances provided guice, instances populated necessary dependencies marked @inject annotation.

however, when run above code bootstrap jersey, see error following:

org.glassfish.hk2.api.unsatisfieddependencyexception: there no object available injection @ systeminjecteeimpl(.... 

apparently happening jersey also trying resolve @inject dependencies on rest controller instance, because sees same @inject annotations guice does.

i don't want jersey dependency injection. think there bridges jersey guice, based on program requirements can't show here, need create rest controller instances myself , ask guice injector them.

my question: how can disable dependency injection in jersey?

i'm using javax.inject.inject annotation. maybe can use com.google.inject.inject annotation instead, thinking jersey won't looking these annotations. still, rather turn off dependency injection in jersey. how can that?

thank you!!

first, solution:

public class guicejerseymanualbridge extends io.dropwizard.application<configuration> {      @override     public void run(configuration configuration, environment environment) throws exception {         jerseyenvironment jersey = environment.jersey();          // create guice env , dependencies         injector = guice.createinjector(new abstractmodule() {             @override             protected void configure() {                 map<string, string> props = new hashmap<>();                 props.put("testme", "hello world guice inject test");                 names.bindproperties(binder(), props);                 bind(helloresource.class).in(singleton.class);             }         });          // instance          helloresource resourceinstance = i.getinstance(helloresource.class);         jersey.register(new abstractbinder() {              @override             protected void configure() {                 // teach jersey guice dependency                  bind(resourceinstance).to(helloresource.class);             }         });          jersey.register(helloresource.class); // register resource - jersey discover binding     }      @override     public void initialize(bootstrap<configuration> bootstrap) {         super.initialize(bootstrap);     }      public static void main(string[] args) throws exception {         new guicejerseymanualbridge().run("server", "/home/artur/dev/repo/sandbox/src/main/resources/config/test2.yaml");     }      @path("test")     @produces(mediatype.application_json)     public static class helloresource {           @inject         @named("testme")         private string teststring;          public helloresource() {             system.err.println("i created now");         }          @get         @path("test")         public string test(string x) {             return teststring;         }      }  } 

please disregard dropwizard setup. uses jersey , uses guice, registering part same.

you facing 2 dilemmas here:

  1. jersey di attempt inject fields in object. because doesn't realise there object done. assumes has inject fields.

so, solution above following:

bind guice bean jersey environment. enable jersey find bean created. since bound jersey env, not attempt re-init bean, rather treat valid object. work, need register resource class argument (triggers jersey search registered bean, or create 1 if needed)

the other solution move injects constructor (generally practice avoid field injection). because register object, illegal call constructor again. therefore jersey not attempt injection (since there none do)

finally, don't know how requirements work, want manually create guice-jersey bridge. teaching beans jersey in way demonstrate guice-jersey bridge does, exception fixes little edge cases (as 1 see now). highly recommended implement bridge.

what bridge delegate creation of beans guice. means (1) ask guice instance , create 1 (if guice returns no instance).

hiope helps,

cheers! artur


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -