Swagger Codegen, Maven Plugin: Restrict Server Generation

Richard Joaquin Mahler picture Richard Joaquin Mahler · May 16, 2016 · Viewed 7.5k times · Source

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>

Answer

jbx picture jbx · Feb 13, 2018

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.