maven 如何避免在swagger代码生成器接口中实现默认方法?

jv2fixgn  于 2022-11-02  发布在  Maven
关注(0)|答案(5)|浏览(211)

我想避免maven插件swagger代码生成的接口中的“默认”实现。例如,使用petstore swagger:http://petstore.swagger.io/v2/swagger.json
我用Maven插件生成界面:

<plugin>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>2.2.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>./src/main/resources/swagger/api.yml</inputSpec>
                        <language>spring</language>
                        <generateSupportingFiles>false</generateSupportingFiles>
                        <configOptions>
                            <interfaceOnly>true</interfaceOnly>
                            <java8>true</java8>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>

我PetApi.java使用方法的默认实现生成了类似www.example.com的接口:

default ResponseEntity<Void> addPet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true )  @Valid @RequestBody Pet body) {
    // do some magic!
    return new ResponseEntity<Void>(HttpStatus.OK);
    }

我想避免它像

ResponseEntity<Void> addPet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true )  @Valid @RequestBody Pet body);

有可能做到吗?
2020年3月更新:
根据新的OpenAPI工具openapi-generator
spring有一个选项(不推荐使用language,请使用generatorName
skipDefaultInterface
https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/spring.md

gtlvzcf8

gtlvzcf81#

对于“spring”语言,“java8”参数既用于表示默认接口的使用,也用于表示Java8的一般使用,例如,当使用Java8数据库时。
相关票证:
https://github.com/swagger-api/swagger-codegen/issues/8045
https://github.com/swagger-api/swagger-codegen/issues/5614

dw1jzc5e

dw1jzc5e2#

我解决了配置同一插件的两个执行。一个配置为只生成模型类(java8=true和dateLibrary=java8-localdatetime),另一个配置为只生成API接口(java=false和dateLibrary为空)。

<plugin>
   <groupId>io.swagger.codegen.v3</groupId>
   <artifactId>swagger-codegen-maven-plugin</artifactId>
   <version>3.0.8</version>
   <executions>
      <execution>
         <id>gera-api-model</id>
         <goals>
            <goal>generate</goal>
         </goals>
         <configuration>
            <inputSpec>${project.basedir}/src/main/openapi-spec/openapi.yaml</inputSpec>
            <language>spring</language>

            <generateModels>true</generateModels>
            <generateApis>false</generateApis>
            <configOptions>
               <dateLibrary>java8-localdatetime</dateLibrary>
               <java8>true</java8> 
             </configOptions>
         </configuration>
      </execution>
      <execution>
         <id>gera-api-interface</id>
         <goals>
            <goal>generate</goal>
         </goals>
         <configuration>
            <inputSpec>${project.basedir}/src/main/openapi-spec/openapi.yaml</inputSpec>
            <language>spring</language>
            <generateModels>false</generateModels>
            <generateApis>true</generateApis>
            <configOptions>
               <java8>false</java8>
            </configOptions>
         </configuration>
      </execution>
   </executions>
   <configuration>
      <inputSpec>${project.basedir}/src/main/openapi-spec/openapi.yaml</inputSpec>
      <language>spring</language>
      <output>${project.build.directory}/generated-sources</output>
      <apiPackage>br.com.acme.myproject.api</apiPackage>
      <modelPackage>br.com.acme.myproject.model</modelPackage>
      <library>spring-mvc</library>
      <generateApiDocumentation>false</generateApiDocumentation>
      <generateModelDocumentation>false</generateModelDocumentation>
      <generateSupportingFiles>false</generateSupportingFiles>
      <generateApiTests>false</generateApiTests>
      <generateModelTests>false</generateModelTests>
      <configOptions>
         <bigDecimalAsString>true</bigDecimalAsString>
         <serializableModel>true</serializableModel>
         <reactive>false</reactive>
         <interfaceOnly>true</interfaceOnly>
      </configOptions>
   </configuration>
   <dependencies>
      <dependency>
         <groupId>io.swagger.codegen.v3</groupId>
         <artifactId>swagger-codegen-generators</artifactId>
         <version>1.0.8</version>
      </dependency>
   </dependencies>
</plugin>

注意:我使用的版本'v3'的插件。

hpxqektj

hpxqektj3#

根据documentation,以下应解决该问题:

<configOptions>
     <skipDefaultInterface>true</skipDefaultInterface>
</configOptions>
y3bcpkx1

y3bcpkx14#

我设法< java8 >< /java8 >在swagger codegen派生的项目中使用false来避免这些默认方法:https://github.com/OpenAPITools/openapi-generator
适合我的示例:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>4.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${maven.multiModuleProjectDirectory}/api/target/generated/swagger-api-spec/swagger.json</inputSpec>  
                            <language>spring</language>
                            <library>spring-boot</library>
                <skipValidateSpec>true</skipValidateSpec>
                <generateSupportingFiles>true</generateSupportingFiles>
                <configOptions>
                    <sourceFolder>src/gen/java/main</sourceFolder>
                    <java8>false</java8>
                    <dateLibrary>java8</dateLibrary>
                    <interfaceOnly>false</interfaceOnly>
                    <groupId>com.company.groupid</groupId>
                    <artifactId>${project.artifactId}</artifactId>
                    <artifactVersion>${project.version}</artifactVersion>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>
jhiyze9q

jhiyze9q5#

有两种不同的插件可用于基于API规范文档生成代码:

  1. io.swagger:swagger-codegen-maven-plugin(最新版本中为io.swagger.codegen.v3:swagger-codegen-maven-plugin
  2. org.openapitools:openapi-generator-maven-plugin
    每一个都有一套不同配置的发电机。
    最初的问题是关于第一个问题的,但是作者找到的解决方案是关于第二个问题的。事实上,现在用于swagger-codegen-maven-plugin的Spring生成器也有一个类似的选项:defaultInterfaces。下面的示例对我很有效:
<plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>3.0.35</version>
    <executions>
    <execution>
      <id>generate-provider-v1</id>
      <phase>generate-resources</phase>
      <goals>
      <goal>generate</goal>
      </goals>
    </execution>
    </executions>
    <configuration>
    <inputSpec>${project.basedir}/src/main/resources/myopenapi.yaml</inputSpec>
    <output>${project.basedir}</output>
    <language>spring</language>
    <library>spring-boot</library>
    <modelPackage>my.package.model</modelPackage>
    <apiPackage>my.package.api</apiPackage>
    <configOptions>
      <defaultInterfaces>false</defaultInterfaces>
      <interfaceOnly>true</interfaceOnly>
      <dateLibrary>java8</dateLibrary>
    </configOptions>
    <modelNameSuffix>Dto</modelNameSuffix>
    <generateSupportingFiles>false</generateSupportingFiles>
    </configuration>
  </plugin>

相关问题