Mar*_*nek 7 spring-boot openapi openapi-generator
我有一个 Spring Boot 应用程序,我使用 openapi-generator-maven-plugin 来生成其余客户端。我想要一个在运行时更改 url 的选项。
其余服务器的 url 现在硬编码在以下 OpenAPI 定义片段中:
openapi: 3.0.1
info:
title: OpenAPI definition
version: v0
servers:
- url: 'http://localhost:8080'
description: Generated server url
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
这段代码是生成的
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
我需要配置这个属性。知道怎么做吗?
您可以创建生成的 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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2833 次 |
| 最近记录: |