Quarkus: Injection doesn't work in ClientHeadersFactory

Created on 30 Oct 2019  路  6Comments  路  Source: quarkusio/quarkus

Initial discussion can be found on StackOverflow

Describe the bug
Injecting a bean into a class that extends ClientHeadersFactory doesn't work.

Expected behavior
I would like to have an instance of my class injected by the container

Actual behavior
The object is null

To Reproduce
Here is my code:

public class StoresClientHeadersFactoryImpl implements ClientHeadersFactory {

    @Inject
    Configuration applicationConfig;

    @Override
    public MultivaluedMap<String, String> update(MultivaluedMap<String, String> inbound, MultivaluedMap<String, String> outbound) {

        MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
        headers.putAll(inbound);
        headers.putAll(outbound);
        headers.add("ApiKey", applicationConfig.getKey());
        return headers;
    }
}
@ApplicationScoped
public class Configuration {

    @ConfigProperty(name = "apiKey.stores")
    private String apiStoreApiKey;

    public String getKey() {
        return apiStoreApiKey;
    }

    public static String getApiStoreApiKey() {
        return CDI.current().select(Configuration.class).get().getKey();
    }
}
@Path("/stores")
@RegisterRestClient
@RegisterClientHeaders(value = StoresClientHeadersFactoryImpl.class)
public interface StoresService {

    @GET
    @Produces("application/json")
    Stores getStores();
}

Configuration

# Configuration file
com.acme.StoresService/mp-rest/url = <my-url>
com.acme.StoresService/mp-rest/scope = javax.inject.Singleton
apiKey.stores = <my-value>

Environment (please complete the following information):

  • Output of uname -a or ver: Darwin mbp.local 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64
  • Output of java -version: openjdk version "11.0.3" 2019-04-16
  • GraalVM version (if different from Java): -
  • Quarkus version or git rev: 0.25.0
kinenhancement

Most helpful comment

@kenfinnigan - it was me :)

@gsmet - about this injection point. At the moment there is no clean and elegant alternative to generate headers for annotated client interface. Other things like @ClientHeaderParam annotation requires either interface default method or static method, so again it's not possible to inject beans. So, implementation of the client for any API that requires e.g authentication tokens or some additional headers (that usually needs to be provided by app config) requires to pass those values into each client method which becomes quite cumbersome.

All 6 comments

@kenfinnigan @asoldano This will require a change in resteasy-client-microprofile since currently ClientHeaderProviders doesn't have a way to delegate the creation of a ClientHeadersFactory.

Based on the current MP REST Client specification, it doesn't define whether injection should be possible within the factory.

I believe there are other types where injection isn't supported either for MP REST Client.

I think we'd need this clarified in the spec first

Makes sense.

My perspective is that this sort of injection is very useful for users.

@kenfinnigan do you have the spec side under control on that one?

I'm not totally sure supporting more injection points than the spec strictly requires could be seen as a problem. But if you think it is, it would be nice to get this ball moving.

Thanks!

@kenfinnigan - it was me :)

@gsmet - about this injection point. At the moment there is no clean and elegant alternative to generate headers for annotated client interface. Other things like @ClientHeaderParam annotation requires either interface default method or static method, so again it's not possible to inject beans. So, implementation of the client for any API that requires e.g authentication tokens or some additional headers (that usually needs to be provided by app config) requires to pass those values into each client method which becomes quite cumbersome.

Was this page helpful?
0 / 5 - 0 ratings