Jaxrs-api: Inject Client via CDI

Created on 15 Mar 2019  路  5Comments  路  Source: eclipse-ee4j/jaxrs-api

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();
    }
}
api enhancement

Most helpful comment

@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! :-)

All 5 comments

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

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

@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! :-)

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?

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MarkusKull picture MarkusKull  路  5Comments

jslsolucoes picture jslsolucoes  路  8Comments

rfelcman picture rfelcman  路  6Comments

kwsutter picture kwsutter  路  11Comments

mkarg picture mkarg  路  10Comments