web-dev-qa-db-de.com

Wie generiere ich Client-Code mit swagger-codegen-plugin (maven)?

Ich muss einen Server-Stub-Code in Eclipse mit swagger-codegen-plugin (für maven) generieren. kannst du bitte helfen, wie es geht? und welche Konfiguration dazu (in pom.xml) erforderlich ist. 

2
rajesh reddy SR

ich habe diese Antwort gefunden. Es gibt nichts, was Sie brauchen, um pom.xml wie folgt zu ändern.

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>
2
rajesh reddy SR
<!-- 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>

Offizielle Parameterbeschreibung hier

Swagger-Syntaxspezifikation hier

Bei diesem Beispiel-Plugin generieren die verwendeten Swagger-Datenmodelldateien (yaml) Modellklassen, die in den Controllern verwendet werden. 

0
Vladimir Azarov
0
kolobok