Quarkus – 如何将由OpenAPI生成的API用作Quarkus REST客户端?

huangapple go评论54阅读模式
英文:

Quarkus - how to use OpenAPI generated API as Quarkus rest client?

问题

我有来自另一个项目的YAML定义,将为我生成一个ExternalAPI.java。我正在使用Maven的OpenAPI插件。

现在,我还可以在Quarkus中使用@RestClient,定义@RegisterRestClient(configKey="foo")并启用注入;还有@RegisterClientHeaders来注入一些标头。

现在,如何将两者结合起来?我希望继续使用我提到的注解,同时利用生成的API类。


现在,我正在使用RestClientBuilder

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

可用,但有点奇怪;我需要自己创建实例。

英文:

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.

答案1

得分: 2

你可以创建一个接口它扩展了生成的 `ExternalAPI` 接口并用 `@RegisterRestClient``@RegisterClientHeaders` 进行注释

```java
@RegisterRestClient(configKey="foo")
@RegisterClientHeaders(MyHeadersFactory.class)
public interface MyExternalAPI extends ExternalAPI {
// 定义一些内容
}

然后在任何服务中进行注入:

public class MyService {
    @Inject
    MyExternalAPI externalAPI;

    public void doSomething() {
        // 使用 externalAPI 实例
    }
}

现在你可以使用你提到的注解,并利用生成的 API 类。Quarkus 将为你处理 MyExternalAPI 的实例化。


<details>
<summary>英文:</summary>

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.

</details>



# 答案2
**得分**: 1

你基本上在询问[这个][1]功能。
不幸的是,它尚未被实现。

  [1]: https://github.com/quarkusio/quarkus/issues/32189

<details>
<summary>英文:</summary>

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


  [1]: https://github.com/quarkusio/quarkus/issues/32189

</details>



huangapple
  • 本文由 发表于 2023年4月17日 21:08:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定