如何根据yml文件生成resttemplate?

How to generate resttemplate based on yml file?

我使用的是openapi-generator-gradle-plugin。这是我的 build.gradle:

buildscript {
    repositories {
        mavenLocal()

        jcenter {
            url "http://nexus.ca.sbrf.ru:8081/nexus/content/repositories/jcenter"
        }
        maven {
            url "http://nexus.ca.sbrf.ru:8081/nexus/content/repositories/central"
        }
        maven { url 'http://nexus.ca.sbrf.ru:8081/nexus/content/repositories/Atlassian_proxy' }
        maven { url 'http://nexus.ca.sbrf.ru:8081/nexus/content/groups/public' }

        dependencies {
            classpath 'org.openapitools:openapi-generator-gradle-plugin:4.3.0'
            classpath "org.springframework.boot:spring-boot-gradle-plugin:2.2.1.RELEASE"
            classpath "io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE"
        }
    }
}


apply plugin: 'java'
apply plugin: 'org.openapi.generator'
apply plugin: "maven-publish"
apply plugin: "io.spring.dependency-management"
apply plugin: "org.springframework.boot"

dependencies {
    compile 'org.springframework:spring-context:5.3.0'
    compile 'org.springframework:spring-web:4.1.6.RELEASE'
    compile 'javax.servlet:javax.servlet-api:4.0.1'
    compile 'org.openapitools:jackson-databind-nullable:0.2.1'
    compile 'io.swagger:swagger-core:1.5.21'
    compile 'javax.annotation:javax.annotation-api:1.3.1'
}

openApiGenerate {
    generatorName = "spring"
    library = "resttemplate"
    inputSpec = "$rootDir/openapi-contracts/src/main/resources/openapi/api.yml".toString()
    outputDir = "$project.buildDir/generated/openapi"
    apiPackage = "ru.openapi.api.v1"
    modelPackage = "ru.openapi.model.v1"
    enablePostProcessFile = true
    configOptions = [
            dateLibrary         : "java8",
            useBeanValidation   : "true",
            enableBuilderSupport: "true",
            interfaceOnly       : "true",
            delegatePattern     : "true"
    ]
}

sourceSets {
    main {
        java {
            srcDirs "build/generated/openapi/src/main/java"
        }
    }
}

正在生成服务器代码,一切正常,但我希望为其生成resttemplate。

为此我已经指出:

library = "resttemplate"

我收到这个错误:

Unknown library: resttemplate Available libraries: spring-boot spring-mvc spring-cloud

我该如何解决这个问题?

不知道如何使用 gradle,但在 maven 中它会是这样的:

      <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>${openapi.plugin.version}</version>
            <executions>
                <execution>
                    <id>${your-id}</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <skipIfSpecIsUnchanged>true</skipIfSpecIsUnchanged>
                        <inputSpec>${project.basedir}/swagger.json</inputSpec>
                        <generatorName>java</generatorName>
                        <library>resttemplate</library>
                        <apiPackage>your.package</apiPackage>
                        <modelPackage>your.package</modelPackage>
                        <generateApis>true</generateApis>
                        <generateApiDocumentation>false</generateApiDocumentation>
                        <generateApiTests>false</generateApiTests>
                        <generateModels>true</generateModels>
                        <generateModelDocumentation>false</generateModelDocumentation>
                        <generateModelTests>false</generateModelTests>
                        <generateSupportingFiles>true</generateSupportingFiles>
                        <configOptions>
                            <sourceFolder>src/main/java</sourceFolder>
                            <java8>true</java8>
                            <dateLibrary>java8</dateLibrary>
                            <useTags>true</useTags>
                            <interfaceOnly>false</interfaceOnly>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>

希望对你有所帮助。 也许问题是 resttemplate 在 spring-web dependency

要使用 resttemplate 库,您必须将 generatorName 设置为“java”

一种解决方案是使用 spring 生成器,它会为您提供带注释的界面和 类.

created a library (and there are others),给定带注释的接口,将在运行时构造一个类型安全的客户端。

final StoreApi storeApi = SpringRestTemplateClientBuilder
  .create(StoreApi.class)
  .setRestTemplate(restTemplate)
  .setUrl("https://...")
  .build();

// This will become a rest-call
storeApi.deleteOrder(1234L);