openapi-generator-maven-plugin:在运行时更改 rest 客户端基本路径

我有一个 Spring Boot 应用程序,我使用 openapi-generator-maven-plugin 来生成 rest 客户端。我想有一个在运行时更改 url 的选项。

其余服务器的 url 现在硬编码在以下 OpenAPI 定义片段中:

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
  - url: 'http://localhost:8080'
    description: Generated server url

maven插件的配置:

            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>4.3.1</version>
<execution>
                        <id>vydejClient</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}/src/main/resources/manualni_kodovani_vydej.yaml
                            </inputSpec>
                            <generatorName>java</generatorName>
                            <generateApiTests>false</generateApiTests>
                            <generateModelTests>false</generateModelTests>
                            <configOptions>
                                <dateLibrary>java8</dateLibrary>
                            </configOptions>
                            <library>resttemplate</library>
                            <typeMappings>
                                <typeMapping>File=org.springframework.core.io.Resource</typeMapping>
                            </typeMappings>
                            <apiPackage>client</apiPackage>
                            <modelPackage>client.model</modelPackage>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

生成此代码

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2020-11-23T14:40:42.232315+01:00[Europe/Prague]")
@Component("ApiClient")
public class ApiClient {
    ...
    private String basePath = "http://localhost:8080";
    ...
     /**
     * Set the base path, which should include the host
     * @param basePath the base path
     * @return ApiClient this client
     */
    public ApiClient setBasePath(String basePath) {
        this.basePath = basePath;
        return this;
    }

}

我需要让这个属性可配置。知道怎么做吗?

stack overflow openapi-generator-maven-plugin: change rest client base path during runtime
原文答案

答案:

作者头像

您可以创建生成的 api 客户端类的 bean 和配置了该类所需端点的 ApiClient bean。

@Configuration 
public class YourGeneratedApiConfig {

  @Value("${rest.clients.yourGeneratedApi.endpoint}")
  private String endpoint;

  @Bean public YourGeneratedApi yourGeneratedApi() {
    return new YourGeneratedApi(apiClient); 
  }

  @Bean public ApiClient apiclient(RestTemplate restTemplate) {
    ApiClient apiclient = new ApiClient(restTemplate);
    apiclient.setBasePath(endpoint);
    return apiclient; 
  }

  @Bean public RestTemplate restTemplate (RestTemplateBuilder restTemplateBuilder) {
    return restTemplateBuilder.build();
  }

}