Eria
Eria

Reputation: 3172

Define client target URL at runtime using Quarkus & Resteasy

I need to send HTTP requests from my Quarkus application. Following this guide, I have this RestClient:

@Path("/v1")
@RegisterRestClient
public interface CountriesService {

    @GET
    @Path("/name/{name}")
    Set<Country> getByName(@PathParam String name);
}

In the Path annotation, I can configure the path. But the domain/url to call is defined in a configuration file, according to this paragraph.

# Your configuration properties
org.acme.rest.client.CountriesService/mp-rest/url=https://restcountries.eu/rest # 
org.acme.rest.client.CountriesService/mp-rest/scope=javax.inject.Singleton #

In my case, I need this URL to be defined programmatically at runtime, as I receive it as a callback URL.

Is there a way to do that?

Upvotes: 7

Views: 3392

Answers (2)

newur
newur

Reputation: 520

This became possible with Quarkus version 3.16. You can use the annotation @io.quarkus.rest.client.reactive.Url on a method parameter.

The official documentation of this feature is a bit short at the moment, but you find a good example in the official tests: UrlOnStringParameterTest.java. It looks like this:

@Path("test")
@RegisterRestClient
public interface Client {

    @Path("count")
    @GET
    String test(@Url String uri);
}

// called via...

@RestClient
Client client;

public void callCustomUrl(String yourCustomUrl) {
    String result = client.test(yourCustomUrl);
    // do stuff
}

Upvotes: 2

Michał Szynkiewicz
Michał Szynkiewicz

Reputation: 797

Quarkus Rest Client, and Quarkus Rest Client Reactive, implement the MicroProfile Rest specification and as such allow creating client stubs with RestClientBuilder programmatically, e.g.:

public class SomeService {
   public Response doWorkAgainstApi(URI apiUri, ApiModel apiModel) {
       RemoteApi remoteApi = RestClientBuilder.newBuilder()
            .baseUri(apiUri)
            .build(RemoteApi.class);
       return remoteApi.execute(apiModel);
   }
}

See https://download.eclipse.org/microprofile/microprofile-rest-client-2.0/microprofile-rest-client-spec-2.0.html#_sample_builder_usage

You cannot achieve this with client created with the @RegisterRestClient annotation

Upvotes: 8

Related Questions