1

I have YAML definition from another project which will generate an ExternalAPI.java for me. I am using OpenAPI plugin of Maven.

Now, I also can do @RestClient in Quarkus, defining @RegisterRestClient(configKey="foo") and enable injection; also, @RegisterClientHeaders to inject some header.

Now, how to combine both? I want to keep using the annotations I mentioned, while making use of the generated API class.


Now, I am using RestClientBuilder:

RestClientBuilder.newBuilder()
        .baseUri(new URI(someUrl))
        .register((ClientRequestFilter) context -> context.getHeaders().put("MyToken", Collections.singletonList(token)))
        .build(ExternalAPI.class);

Usable, but little bit strange; I need to create the instance myself.

WesternGun
  • 11,303
  • 6
  • 88
  • 157

2 Answers2

2

you can create an interface that extends the generated ExternalAPI interface and annotate it with @RegisterRestClient and @RegisterClientHeaders

@RegisterRestClient(configKey="foo")
@RegisterClientHeaders(MyHeadersFactory.class)
public interface MyExternalAPI extends ExternalAPI {
// define something
}

and now inject it in any service

public class MyService {
    @Inject
    MyExternalAPI externalAPI;

    public void doSomething() {
        // use the externalAPI instance
    }
}

Now you can use the annotations you mentioned and make use of the generated API class. Quarkus will handle the instantiation of MyExternalAPI for you.

M.Abdullah Iqbal
  • 219
  • 1
  • 17
  • 1
    This works for me, thanks. Even though I think this is a workaround and inheritance is not what I prefer, it is a solution. Additionally it solves RESTEASY004687 problem(Quarkus not closing the RestClient built underneath). – WesternGun Apr 19 '23 at 11:00
  • it's an honor that you got the answer – M.Abdullah Iqbal Apr 19 '23 at 16:42
  • 1
    One thing to add is that, `@Produces` and `@Consumes` added to the extended class does **NOT** really changes the `Content-Type` and `Accept` header of request, if they specify a different value than the super one. If you want to change these headers, they need to be added explicitly. – WesternGun Apr 24 '23 at 15:13
  • Yes, that's correct. The `@Produces` and `@Consumes` annotations are used to specify the media types that a REST endpoint can produce or consume. However, they do not automatically change the Content-Type and Accept headers of the request or response. ``` type = "Content-Type", "application/json" or type = "Accept", "application/xml" ```` (that's not how we declare the variable in Java, That's just an example) for that purpose you just need to change the ```target.request().header().>()``` – M.Abdullah Iqbal Apr 26 '23 at 06:29
1

You are essentially asking for this feature. Unfortunately it has not yet been implemented.

geoand
  • 60,071
  • 24
  • 172
  • 190
  • 1
    Thanks, glad to know that there is already an issue created for the dev team; but the link is not visible for me at this moment, it shows "Looks like something went wrong!" – WesternGun Apr 18 '23 at 09:24
  • Yeah, Github is experiencing an outage as we write – geoand Apr 18 '23 at 09:34