繁体   English   中英

Swagger Codegen,Maven插件:限制服务器生成

[英]Swagger Codegen, Maven Plugin: Restrict Server Generation

我想使用swagger-codegen maven插件为我的API生成JAX-RS服务器存根,但我想使用自己的服务实现类,而不是生成的服务实现类。 有没有办法生成除了这个类以外的所有东西? 对于我的API,该工具生成四个api类:ProfilesApi,ProfilesApiService,ProfilesApiServiceFactory和ProfilesApiServiceImpl。

我目前的maven配置:

                     <configuration>
                        <inputSpec>src/main/resources/Profile.json</inputSpec>
                         <language>jaxrs</language>
                        <configOptions>
                            <dateLibrary>java8</dateLibrary>
                        </configOptions>
                        <models>Profile,PageInformation,ProfileResult</models>
                        <modelPackage>com.myApp.profile-api-model</modelPackage>
                        <apiPackage>com.myApp.profile-api-webapp</apiPackage>
                        <library>jersey2</library>
                        <environmentVariables>
                            <!-- change default client library (here until plugin 2.1.5). Doesn't seem to work! -->
                            <library>jersey2</library>
                            <!-- generate all models -->
                            <models></models>
                            <!-- generate all APIs -->
                            <apis></apis>
                            <!-- generate just the supporting files that are Java source code (not project build files) -->
                            <supportingFiles>ApiException.java,ApiOriginFilter.java,ApiResponseMessage.java,JacksonJsonProvider.java,LocalDateProvider.java,LocalDateTimeProvider.java,NotFoundException.java,StringUtil.java,web.xml,ProfilesApi.java,ProfilesApiService.java,ProfilesApiServiceFactory.java</supportingFiles>
                        </environmentVariables>
                    </configuration>

做到这一点的合适的方法是通过两个配置选项, <generateSupportingFiles>在配置和<interfaceOnly><configOptions> <generateSupportingFiles>不生成可执行入口点, pom.xml等,而<interfaceOnly>标志不生成服务的实现,只生成接口。 通过这种方式,您可以拥有自己的可执行类来执行其他操作,自己的pom.xml和自己的服务实现,并且可以使用mvn clean package重新生成API和模型。

我不确定Jersey模板是否检查<interfaceOnly ,但Spring Boot和CXF JAX-RS模板是否可以。

在您的maven pom.xmlswagger-codegen-maven-plugin的build插件中的<configuration>需要如下所示:

<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
    <interfaceOnly>true</interfaceOnly>
...
</configOptions>

不幸的是,这些配置选项没有很好的文档记录,你必须做大量的研究,并在一些github问题上偶然发现它们。

您必须使用codegen ignore文件来阻止生成实现类

这里的解决方案有两个步骤。

  1. 将** / * Controller.java或** / * Impl.java添加到.swagger-codegen-ignore文件(在target / generated-sources中)。 根据使用的语言,* Controller.java或* Impl.java文件中提供了默认实现。 从生成中排除默认实现后,您可以在自己的类中实现生成的接口。 您自己的类中的代码将不会在mvn clean上刷新。

  2. swagger-codegen-ignore文件本身是一个自动生成的文件,因此当您执行mvn clean时,在步骤1中添加的任何内容都会刷新。 为避免这种情况,请将以下插件添加到您的pom中:

     <plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.6.1</version> <configuration> <excludeDefaultDirectories>true</excludeDefaultDirectories> <filesets> <fileset> <directory>${project.build.directory}</directory> <excludes> <exclude>generated-sources/.swagger-codegen-ignore</exclude> </excludes> </fileset> </filesets> </configuration> </plugin> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM