rest
rest copied to clipboard
Inject Client via CDI
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();
}
}
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.