rest icon indicating copy to clipboard operation
rest copied to clipboard

Inject Client via CDI

Open sdaschner opened this issue 5 years ago • 5 comments

It would be a nice addition if the JAX-RS Client type can be injected via @Inject (see #569, #60, #639) which results in the same result as ClientBuilder.newClient(). However, by doing so we facilitate to mock the JAX-RS client out of classes in test scopes.

Additionally, the ClientBuilder could also be injectable, if users want to use injection with further configuration and still enable the implementation to be mocked. The issue with having client calls in test scopes is that the static ClientBuilder.newBuilder() attempts to load via SPI and is hard to mock (being static).

Example for the second suggestion:

public class Foobar {

    @Inject
    ClientBuilder clientBuilder;

    private Client client;

    @PostConstruct
    private void initClient() {
        client = clientBuilder
                .connectTimeout(1, TimeUnit.SECONDS)
                .readTimeout(3, TimeUnit.SECONDS)
                .build();
    }

    // OR via constructor-based injection

    private Client client;
    
    @Inject
    public Foobar(ClientBuilder clientBuilder) {
        client = clientBuilder
                .connectTimeout(1, TimeUnit.SECONDS)
                .readTimeout(3, TimeUnit.SECONDS)
                .build();
    }
}

sdaschner avatar Mar 15 '19 10:03 sdaschner

In line with our roadmap we should cover this in JAX-RS 3.0.

mkarg avatar Apr 25 '19 11:04 mkarg

Is this something we could start working on or we should wait 'till 2.2 is out?

jeyvison avatar Jul 06 '19 20:07 jeyvison

@jeyvison Great to have you on board! Please find the roadmap in our wiki. CDI will be part of JAX-RS 3.0, which is rather far away. So if you start working on that, it could happen that you need to fix git conflicts from time to time. If you don't fear that, have fun coding! :-)

mkarg avatar Jul 07 '19 09:07 mkarg

Could you please foresee in the spec all the extension/configuration points needed by a standalone client (not deployed in a EE container) to use CDI-SE?

kalgon avatar Oct 20 '19 21:10 kalgon

Could you please foresee in the spec all the extension/configuration points needed by a standalone client (not deployed in a EE container) to use CDI-SE?

I think we could cover that with JAX-RS 3.0. JAX-RS 2.x does not regulate CDI SE.

mkarg avatar Oct 20 '19 22:10 mkarg