It's possible to change server in a HTTP micronaut declarative client?

Viewed 758

I'm using a declarative http client of Micronaut to retrieve data from an API. But now I need to dinamically change the server address at runtime. It's possible ?

Example:

@Client("${http.client.url}")
@Header(name="Accept-Encoding", value="gzip, deflate, br")
public interface CatalogClientApi {

It's possible to change "${http.client.url}" somehow ? Or I have to switch to the low level http client?

3 Answers

@Client annotation is injecting RxHttpClient.

You could do it using declarative approach

URL url = new URL("http://your-url-here.com");
RxHttpClient client = RxHttpClient.create(url);

I have then found a simple solution (a bit ugly):

@Client("/")
@Header(name="Accept-Encoding", value="gzip, deflate, br")
public interface ExampleApi {
    @Post("{+path}/V1/products")
String post(@PathVariable String path, @Header("Authorization") String token, Body product);

The "@Client" has a / placeholder and then the server will be in the path variable used in {+path}. I found a bit misleading using the host here, but it's working perfectly.

You have to switch. Annotations in Micronaut are processed during compile time.

Related