'Getting header value from a config property using @ClientHeaderParam is not working

I am trying to get a header value from the config into the Rest(easy) client using the @ClientHeaderParam annotation as described here https://quarkus.io/guides/rest-client-reactive#custom-headers-support, unfortunately it does not work out. The value is sent as-is, rather than replaced with the corresponding config property Here is roughly what I am doing

@RegisterRestClient
@ClientHeaderParam(name = "Key", value = "${api-key}")
public interface MyClient {

  @POST
  @Path("/api")
  @Consumes(MediaType.APPLICATION_OCTET_STREAM)
  @Produces(MediaType.APPLICATION_JSON)
  Response call(InputStream image);
}

When I invoke the call method and check the request, I see that the Key header has ${api-key} as value, and not the value I have in application.properties for api-key.

Thanks in advance.



Solution 1:[1]

Based on the answer I got on github https://github.com/quarkusio/quarkus/discussions/24418

The issue is related using the wrong dependency. I should be using quarkus-rest-client-reactive while I am using quarkus-rest-client. This is actually weird because it has nothing to do with reactive or not reactive, but it is the way it is.

Solution 2:[2]

As per the documentation of the microprofiles, the annotation ClientHeaderParam does not support reading values from config. Instead we can provide the default method or static method from some sort of utility class. Please refer to the javadoc at https://download.eclipse.org/microprofile/microprofile-rest-client-1.2.1/apidocs/org/eclipse/microprofile/rest/client/annotation/ClientHeaderParam.html

Following is sample code that might be of use in your context:

@RegisterRestClient(baseUri = "http://localhost:8000")
@ClientHeaderParam(name = "Key", value = "{getApiKey}")
@ClientHeaderParam(name = "api-key", value = "{getConfigValue}")
public interface MyRemoteService {

    default String getApiKey() {
        return ConfigProvider.getConfig().getValue("api-key",String.class);
    }

    default String getConfigValue(String key) {
        return ConfigProvider.getConfig().getValue(key,String.class);
    }

    @GET
    @Path("/hello")
    @Produces(MediaType.TEXT_PLAIN)
    String helloWithKeyHeader();
}

Refer to sample code at https://github.com/gopinnath/quarkus-rest-example-parent

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zakaria amine
Solution 2 Gopinath Radhakrishnan