5

I have searched a while on SO and official documentation but I cannot found a way to use directly CDI injection into a JAX-RS client.

I retrieve a client using the builder method and I want to register a WriterInterceptor (or any filter like component) which uses injection to retrieve another bean.

I want to use CDI injection and avoid registering each bean with HK2.

ClientBuilder.newBuilder()
            .register(MyWriter.class)
            .build();

And MyWriter with the injected class.

@Provider
public class MyWriter implements WriterInterceptor {
    private final MyRepo repo;

    @Inject
    public MyWriter(MyRepo repo) {
        this.repo = repo;
    }

    @Override
    public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
        context.proceed();
    }
}

public class MyRepo {

}

I am running in an embedded jetty with Jersey 2 and Weld SE.

Nicolas Henneaux
  • 11,507
  • 11
  • 57
  • 82

2 Answers2

1

Its possible to inject in java se application using wield .

 @Singleton
public class Application {

private static Logger logger = LoggerFactory.getLogger(Application.class);

    @inject
    private SomeOtherBean injectedBean;

public void run() {
    logger.debug("application initialized");
        injectedBean.doSomething();

}

}

inside main initialize weild

import java.io.IOException;

import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;

public class EntryPoint {

  public static void main(String[] args) throws IOException {

   Weld weld = new Weld();
   WeldContainer container = weld.initialize();
   Application application = container.instance().select(Application.class).get();
   application.run();
   weld.shutdown();

  }
}

Have a look at below doc

https://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#_java_se

also below tutorial https://randling.wordpress.com/2011/08/15/cdi-in-java-se/

gladiator
  • 1,249
  • 8
  • 23
  • I already use this type of configuration. My question is about using CDI into a JAX-RS client which does not work since the HK2 injection takes precedence on Weld. – Nicolas Henneaux Dec 19 '16 at 06:27
0

If I understand everything correctly, this has already been asked and answered. In a nutshell: you have to override the default behaviour of the H2K Binder, so it reaches for the Weld Bean Manager. You don't have to register every Bean with H2K later on.

Edit: to contain everything in the post, so you don't have to read the comments:

Community
  • 1
  • 1
D. Kovács
  • 1,232
  • 13
  • 25
  • This only applies for server side, I have implemented what is describe in the answer you reference. – Nicolas Henneaux Dec 19 '16 at 10:21
  • Indeed, you are correct. Shouldn't the combination of the [standard Client-side injection providers](https://jersey.java.net/documentation/latest/client.html#d0e5194) with the [Jersey Weld extentsion](https://github.com/jersey/jersey/blob/master/ext/cdi/jersey-weld2-se/src/main/java/org/glassfish/jersey/weld/se/WeldHk2LocatorManager.java) do the trick on the client side? – D. Kovács Dec 19 '16 at 10:31
  • I did not manage to replace the locator for the client and there is still the manual look up working but I want to use the injection directly. – Nicolas Henneaux Dec 19 '16 at 10:33
  • Seems doable, but hacky, as far as I see the Dropwizard guys solved this: see [this github ticket](https://github.com/dropwizard/dropwizard/issues/1026) – D. Kovács Dec 19 '16 at 10:49