Skip to content

Instantly share code, notes, and snippets.

@Anebrithien
Created March 6, 2024 08:48
Show Gist options
  • Save Anebrithien/7c1b41246cbf25292137e17012fdb372 to your computer and use it in GitHub Desktop.
Save Anebrithien/7c1b41246cbf25292137e17012fdb372 to your computer and use it in GitHub Desktop.
Generate openmeteo api client jar from OpenAPI spec yaml files.

How to generate client Jar from OpenAPI spec file

Use openapi-generator-maven-plugin, steps:

  1. Create folder open-meteo-client
    mkdir open-meteo-client
    cd open-meteo-client
  2. Create pom.xml in folder, structure should be like
    .
    ├── pom.xml
    └── src
        ├── main
        │   ├── java
        │   └── resources
        └── test
            └── java
    
  3. run mvn clean install -DskipTests
  4. Add artifact as dependency, use ApiClient like:
      @Bean
      public HistoricalWeatherApiApi historicalWeatherApiApi(
          RestTemplateBuilder restTemplateBuilder,
          @Value("${openmeteo.api-base-url.historical:https://archive-api.open-meteo.com}") String basePath) {
        RestTemplate restTemplate = restTemplateBuilder.build();
        ApiClient apiClient = new ApiClient(restTemplate);
        String baseUrl = properties.getApiBaseUrl().getHistorical();
        apiClient.setBasePath(basePath);
        return new HistoricalWeatherApiApi(apiClient);
      }
      // `@Autowired HistoricalWeatherApiApi` somewhere in your springboot application

PS: I don't know how to merge multi specs, maybe some generated class files were overwrited in this example.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>open-meteo-client</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<executions>
<!--<editor-fold desc="Forecast API client">-->
<execution>
<id>forecast-api</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
https://github.com/open-meteo/open-meteo/raw/${open-meteo-api-specs.version}/openapi.yml
</inputSpec>
<generatorName>java</generatorName>
<apiPackage>${project.groupId}.openmeteo.api</apiPackage>
<modelPackage>${project.groupId}.openmeteo.model</modelPackage>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<configOptions>
<serializationLibrary>jackson</serializationLibrary>
<library>resttemplate</library>
<useJakartaEe>true</useJakartaEe>
</configOptions>
</configuration>
</execution>
<!--</editor-fold>-->
<!--<editor-fold desc="Historical API client">-->
<execution>
<id>historical-api</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
https://github.com/open-meteo/open-meteo/raw/${open-meteo-api-specs.version}/openapi_historical_weather_api.yml
</inputSpec>
<generatorName>java</generatorName>
<apiPackage>${project.groupId}.openmeteo.api</apiPackage>
<modelPackage>${project.groupId}.openmeteo.model</modelPackage>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<configOptions>
<serializationLibrary>jackson</serializationLibrary>
<library>resttemplate</library>
<useJakartaEe>true</useJakartaEe>
</configOptions>
</configuration>
</execution>
<!--</editor-fold>-->
</executions>
</plugin>
</plugins>
</build>
<!-- dependencies is required to all generated classes -->
<dependencies>
<!-- @Nullable annotation -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<!-- HTTP client: Spring RestTemplate -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- JSON processing: jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<!-- set API version here -->
<open-meteo-api-specs.version>1.1.8</open-meteo-api-specs.version>
</properties>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment