Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openapi-generator-maven-plugin: change rest client base path during runtime

I have a Spring Boot Application and I use openapi-generator-maven-plugin for generating rest client. I want to have a option to change url during runtime.

The url of the rest server is now hardcoded in the following snippet of OpenAPI definition:

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

Configuration of the maven plugin:

            <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>

This code is generated

@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;
    }


}

I need to have this attribute configurable. Any idea how to do it?

like image 713
leiblix Avatar asked May 31 '26 05:05

leiblix


1 Answers

You can set up an ApiClient bean modifying its base path, and define a bean for your generated api using this modified ApiClient. This setup allows your application to make API calls with the configured base URL.

@Configuration 
public class YourGeneratedApiConfig {
  
  @Value("${rest.clients.example.url}")
  private String exampleUrl;

  // Create a RestTemplate bean using RestTemplateBuilder
  @Bean 
  public RestTemplate restTemplate (RestTemplateBuilder restTemplateBuilder) {
    return restTemplateBuilder.build();
  }
  
  // Create an ApiClient bean and overwrite its base path to the configured one
  @Bean 
  public ApiClient apiclient(RestTemplate restTemplate) {
    ApiClient apiclient = new ApiClient(restTemplate);
    apiclient.setBasePath(exampleUrl);
    return apiclient; 
  }

  // Create a YourGeneratedApi bean using the configured ApiClient
  @Bean 
  public YourGeneratedApi yourGeneratedApi(ApiClient apiclient) {
    return new YourGeneratedApi(apiClient);
  }


  // If you have multiple apis in the same base path 
  // then create AnotherGeneratedApi bean using the same api client
  @Bean 
  public AnotherGeneratedApi anotherGeneratedApi(ApiClient apiClient) {
    return new AnotherGeneratedApi(apiClient);
  }
}

Add the endpoint URL to your application.properties or application.yml file.

rest.clients.exampleUrl.endpoint=https://api.example.com

Create a service class that uses the YourGeneratedApi bean.

@Service
public class YourService {

  private final YourGeneratedApi yourGeneratedApi;

  @Autowired
  public YourService(YourGeneratedApi yourGeneratedApi) {
    this.yourGeneratedApi = yourGeneratedApi;
  }

  public void performApiCall() {
    // Example method call on yourGeneratedApi
    yourGeneratedApi.someApiMethod();
  }
}

Note: If you have generateClientAsBean defined as true (default false) in your openapi-generator-maven-plugin configuration then you should remove it because now we are creating the ApiClient bean manually so we don't want the @Component annotation to be generated in the ApiClient class.

like image 199
Kronen Avatar answered Jun 02 '26 19:06

Kronen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!