5

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 3

4

@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);

4

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.

-1

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

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.