13

I need to generate a server stub code in eclipse using with swagger-codegen-plugin (for maven) . can you please help how to do it ? and what configuration needed for that( in pom.xml).

kolobok
  • 3,835
  • 3
  • 38
  • 54
rajesh reddy SR
  • 386
  • 1
  • 3
  • 14

3 Answers3

14

I found this answer. You just need to change pom.xml like below.

pom.xml.

<properties>
           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
           <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
           <java.version>1.8</java.version>
           <version.swagger.codegen>2.2.1</version.swagger.codegen>
           <yaml.file>${project.basedir}/src/main/resources/Api.yaml</yaml.file>
           <generated-sources-path>${project.build.directory}/generated-sources</generated-sources-path>
           <generated-sources-java-path>main/java</generated-sources-java-path>
       </properties>

<build>
           <plugins>
               <plugin>
                   <groupId>org.springframework.boot</groupId>
                   <artifactId>spring-boot-maven-plugin</artifactId>
               </plugin>
               <plugin>
                   <groupId>io.swagger</groupId>
                   <artifactId>swagger-codegen-maven-plugin</artifactId>
                   <version>${version.swagger.codegen}</version>
                   <configuration>
                       <inputSpec>${yaml.file}</inputSpec>
                       <configOptions>
                           <sourceFolder>${generated-sources-java-path}</sourceFolder>
                       </configOptions>
                       <output>${generated-sources-path}</output>
                   </configuration>
                   <executions>
                       <execution>
                           <id>generate-swagger-spring</id>
                           <phase>generate-sources</phase>
                           <goals>
                               <goal>generate</goal>
                           </goals>
                           <configuration>
                               <language>spring</language>
                               <modelPackage>${project.groupId}.swagger.model</modelPackage>
                               <apiPackage>${project.groupId}.swagger.api</apiPackage>
                               <invokerPackage>${project.groupId}.swagger.invoker</invokerPackage>
                           </configuration>
                       </execution>
                   </executions>
               </plugin>        
               <plugin>
                   <groupId>org.codehaus.mojo</groupId>
                   <artifactId>build-helper-maven-plugin</artifactId>
                   <executions>
                       <execution>
                           <id>add-generated-source</id>
                           <phase>initialize</phase>
                           <goals>
                               <goal>add-source</goal>
                           </goals>
                           <configuration>
                               <sources>
                                   <source>${generated-sources-path}/${generated-sources-java-path}</source>
                               </sources>
                           </configuration>
                       </execution>
                   </executions>
               </plugin>                
           </plugins>   

       <pluginManagement>
           <plugins>
               <plugin>
                   <groupId>org.eclipse.m2e</groupId>
                   <artifactId>lifecycle-mapping</artifactId>
                   <version>1.0.0</version>
                   <configuration>
                       <lifecycleMappingMetadata>
                           <pluginExecutions>
                               <pluginExecution>
                                   <pluginExecutionFilter>
                                       <groupId>io.swagger</groupId>
                                       <artifactId>swagger-codegen-maven-plugin</artifactId>
                                       <versionRange>[${version.swagger.codegen},)</versionRange>
                                       <goals>
                                           <goal>generate</goal>
                                       </goals>
                                   </pluginExecutionFilter>
                                   <action>
                                       <execute />
                                   </action>
                               </pluginExecution>
                           </pluginExecutions>
                       </lifecycleMappingMetadata>
                   </configuration>
               </plugin>
           </plugins>
       </pluginManagement>          
       </build>
rajesh reddy SR
  • 386
  • 1
  • 3
  • 14
  • Note that the plugin management part is eclipse specific. You should only need it for Eclipse support (i.e. plain maven should not need it). But for eclipse this is important and hard to find when just googling for the error it prints when this is not there. – Frank Hopkins Aug 27 '20 at 13:25
  • It seems spring will generate server side code and java will generate client side code. https://github.com/swagger-api/swagger-codegen/issues/5332 – Smart Coder Oct 22 '21 at 15:19
  • You can also use json file instead of yml. Just manually convert yml to json first. – Smart Coder Oct 22 '21 at 18:19
5
<!-- Swagger -->
<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.3.1</version>
    <executions>
        <execution>
            <id>contract-service</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${basedir}/src/main/resources/swagger/rest-data-exchange-format.yaml</inputSpec>
                <artifactId>contract-service</artifactId>
                <output>${basedir}/target/generated-sources</output>
                <language>spring</language>
                <modelPackage>ru.payhub.rest.v1.model</modelPackage>
                <apiPackage>ru.payhub.rest.v1.api</apiPackage>
                <!-- <invokerPackage>ru.payhub.rest.v1.handler</invokerPackage> -->
                <generateSupportingFiles>false</generateSupportingFiles>
                <configOptions>
                    <sourceFolder>src/main/java</sourceFolder>
                    <interfaceOnly>true</interfaceOnly>
                    <library>spring-boot</library>
                    <dateLibrary>${generator.datelibrary}</dateLibrary>
                    <configPackage>ru.payhub.config</configPackage>
                    <singleContentTypes>true</singleContentTypes>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

Official parametrs description here

Swagger syntax specification here

On this example maven plugin, used swagger data-model file (yaml) generate model classes for use it in the controllers.

Mikro Koder
  • 1,056
  • 10
  • 13
  • Could you please tell where did you get those config options? They are not on the page linked. – Aleksandr Erokhin May 21 '21 at 10:46
  • @AleksandrErokhin, which options for your question? All available opts you can found in "Official parametrs description" and into ofiicial page or github project (https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen-maven-plugin/README.md) – Mikro Koder May 25 '21 at 12:01
  • I was looking for the list of `configOptions` and there is none on the official page. – Aleksandr Erokhin May 25 '21 at 12:50
4

Sample configuration for swagger-codegen-maven-plugin is available at https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen-maven-plugin

List of possible languages is available here: https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages

kolobok
  • 3,835
  • 3
  • 38
  • 54