Reputation: 91
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. Is there way to generate everything except this class? For my API, the tool generates four api classes: ProfilesApi,ProfilesApiService, ProfilesApiServiceFactory and ProfilesApiServiceImpl.
My current maven configuration:
<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>
Upvotes: 9
Views: 10941
Reputation: 22128
The proper way to do this is via two configuration options, <generateSupportingFiles>
in the configuration and <interfaceOnly>
in the <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. 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
.
I am not sure if the Jersey templates check for <interfaceOnly
, but the Spring Boot and CXF JAX-RS templates do.
In your maven pom.xml
, your <configuration>
inside the build plugin of the swagger-codegen-maven-plugin
would need to look like this:
<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.
Upvotes: 14
Reputation: 54
There are two steps to the solution here.
Add **/*Controller.java or **/*Impl.java to .swagger-codegen-ignore file ( in target/generated-sources). Depending on the language used the default implementation is provided in a *Controller.java or *Impl.java file. 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.
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. To avoid this add the below plugin to your 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>
Upvotes: -1
Reputation: 466
You must use the codegen ignore file to prevent the implementation classes generation
Upvotes: -2