[英]Swagger Codegen, Maven Plugin: Restrict Server Generation
I want to generate JAX-RS server stubs for my API using the swagger-codegen maven plugin, but I want to use my own service implementation class, instead of the one generated. 我想使用swagger-codegen maven插件为我的API生成JAX-RS服务器存根,但我想使用自己的服务实现类,而不是生成的服务实现类。 Is there way to generate everything except this class? 有没有办法生成除了这个类以外的所有东西? For my API, the tool generates four api classes: ProfilesApi,ProfilesApiService, ProfilesApiServiceFactory and ProfilesApiServiceImpl. 对于我的API,该工具生成四个api类:ProfilesApi,ProfilesApiService,ProfilesApiServiceFactory和ProfilesApiServiceImpl。
My current maven configuration: 我目前的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>
The proper way to do this is via two configuration options, <generateSupportingFiles>
in the configuration and <interfaceOnly>
in the <configOptions>
. 做到这一点的合适的方法是通过两个配置选项, <generateSupportingFiles>
在配置和<interfaceOnly>
在<configOptions>
The <generateSupportingFiles>
does not generate the executable entry point, pom.xml
etc., while the <interfaceOnly>
flag does not generate the implementation of the service, only the interface. <generateSupportingFiles>
不生成可执行入口点, pom.xml
等,而<interfaceOnly>
标志不生成服务的实现,只生成接口。 This way you can have your own executable class doing other stuff, your own pom.xml
and your own service implementation, and you can regenerate the API and the Model as many times as you want using the mvn clean package
. 通过这种方式,您可以拥有自己的可执行类来执行其他操作,自己的pom.xml
和自己的服务实现,并且可以使用mvn clean package
重新生成API和模型。
I am not sure if the Jersey templates check for <interfaceOnly
, but the Spring Boot and CXF JAX-RS templates do. 我不确定Jersey模板是否检查<interfaceOnly
,但Spring Boot和CXF JAX-RS模板是否可以。
In your maven pom.xml
, your <configuration>
inside the build plugin of the swagger-codegen-maven-plugin
would need to look like this: 在您的maven pom.xml
, swagger-codegen-maven-plugin
的build插件中的<configuration>
需要如下所示:
<generateSupportingFiles>false</generateSupportingFiles>
<configOptions>
<interfaceOnly>true</interfaceOnly>
...
</configOptions>
Unfortunately these configuration options are not very well documented, you have to do a lot of research and stumble upon them on some github issue. 不幸的是,这些配置选项没有很好的文档记录,你必须做大量的研究,并在一些github问题上偶然发现它们。
您必须使用codegen ignore文件来阻止生成实现类
There are two steps to the solution here. 这里的解决方案有两个步骤。
Add **/*Controller.java or **/*Impl.java to .swagger-codegen-ignore file ( in target/generated-sources). 将** / * Controller.java或** / * Impl.java添加到.swagger-codegen-ignore文件(在target / generated-sources中)。 Depending on the language used the default implementation is provided in a *Controller.java or *Impl.java file. 根据使用的语言,* Controller.java或* Impl.java文件中提供了默认实现。 Once the default implementation is excluded from generation, you can implement the generated interfaces in your own class. 从生成中排除默认实现后,您可以在自己的类中实现生成的接口。 The code in your own class will not get refreshed on mvn clean. 您自己的类中的代码将不会在mvn clean上刷新。
swagger-codegen-ignore file itself is an auto-generated file hence whatever you add in step 1 gets refreshed when you do a mvn clean. swagger-codegen-ignore文件本身是一个自动生成的文件,因此当您执行mvn clean时,在步骤1中添加的任何内容都会刷新。 To avoid this add the below plugin to your pom: 为避免这种情况,请将以下插件添加到您的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.