简体   繁体   English

OpenAPI 生成器向 ApiClient 构造函数添加/生成注释

[英]OpenAPI generator add/generate annotation to ApiClient constructor

I use the newest OpenAPI generator 6.2.1 ( https://github.com/OpenAPITools/openapi-generator ) to generate an ApiClient with the resttemplate library, which works quite well.我使用最新的 OpenAPI 生成器 6.2.1 ( https://github.com/OpenAPITools/openapi-generator ) 通过 resttemplate 库生成一个 ApiClient,效果很好。

In my application I have now two different RestTemplate beans.在我的应用程序中,我现在有两个不同的 RestTemplate bean。 So Spring does not know which one to use in the ApiClient constructor.所以Spring不知道在ApiClient构造函数中使用哪一个。

Parameter 0 of constructor in com.xyz.ApiClient required a single bean, but 2 were found com.xyz.ApiClient 中构造函数的参数 0 需要单个 bean,但找到了 2 个

There is also a hint to solve the problem:还有解决问题的提示:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed考虑将其中一个 bean 标记为 @Primary,更新消费者以接受多个 bean,或者使用 @Qualifier 来标识应该被消费的 bean

I don't want to mark one of the beans with @Primary because it is not the primary bean wanted to be used.我不想用 @Primary 标记其中一个 bean,因为它不是要使用的主 bean。

I would like to add the @Qualifier to the generated ApiClient constructor like this:我想将 @Qualifier 添加到生成的 ApiClient 构造函数中,如下所示:

    @Autowired
    public ApiClient(@Qualifier("myClientProperties") RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
        init();
    }

How can I add the @Qualifier annotation to the generated constructor?如何将 @Qualifier 注释添加到生成的构造函数?

I read lots of openapi generator documentation but did not find anything helpful.我阅读了很多 openapi 生成器文档,但没有找到任何有用的信息。 There is a solution to add a annotation for models (additionalModelTypeAnnotations in the configOptions of OpenApi configuration).有一个为模型添加注释的解决方案(OpenApi 配置的 configOptions 中的 additionalModelTypeAnnotations)。

I expect to generate a @Qualifier annotation to the ApiClient constructor.我希望为 ApiClient 构造函数生成一个 @Qualifier 注释。

You can disable the component scanning for the generated classes.您可以为生成的类禁用组件扫描。 Assuming, your root package is 'my.root.package' and you generate the classes into 'my.root.package.generated', annotate your App / Config class with the following:假设,您的根 package 是“my.root.package”,并且您将类生成到“my.root.package.generated”中,使用以下注释您的 App / Config class:

@ComponentScan(basePackages = "my.root.package",
           excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX,
                                                  pattern = "my.root.package.generated.*"))

Then, you can create own (qualified) ApiClients based on your rest templates:然后,您可以根据您的 rest 模板创建自己的(合格的)ApiClients:

@Bean("rest-template-1")
public RestTemplate restTemplate1() {
    return new RestTemplate();
}

@Bean("rest-template-2")
public RestTemplate restTemplate2() {
    return new RestTemplate();
}

@Bean("api-client-1")
public ApiClient apiClient1(@Qualifier("rest-template-1") RestTemplate restTemplate) {
    return new ApiClient(restTemplate);
}

@Bean("api-client-2")
public ApiClient apiClient2(@Qualifier("rest-template-2") RestTemplate restTemplate) {
    return new ApiClient(restTemplate);
}

With those different qualified ApiClients, you init your API classes as you need them.使用这些不同的合格 ApiClient,您可以根据需要初始化 API 类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM