使用swagger-codegen-maven-plugin

时间:2018-10-17 14:16:52

标签: java yaml swagger

我使用了swagger-codegen-maven-plugin从schema.yaml生成Java服务器和客户端。但是,我无法生成适当的api.json和swagger-ui.html。

访问http://localhost:8080/v2/api-docs时,我只会得到API描述,而不是完整的json:

{"swagger":"2.0","info":{"description":"A simple API to learn how to write OpenAPI Specification","version":"1.0.0","title":"Simple API","contact":{},"license":{}},"host":"localhost:8080","basePath":"/"}

访问http://localhost:8080/swagger-ui.html#时出现错误页面。

请协助从yaml模式生成api-docs和swagger-ui。 从pom.xml构建:

  <build>
    <plugins>
        <!-- Generate JAX-RS data types & client -->
        <plugin>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <id>generate-rest-client</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <language>spring</language>
                        <inputSpec>${project.basedir}/src/main/resources/schema.yaml</inputSpec>
                        <output>src/gen/java</output>
                        <modelPackage>${base.package}.api.model</modelPackage>
                        <apiPackage>${base.package}.services</apiPackage>
                        <invokerPackage>${base.package}.client</invokerPackage>
                        <addCompileSourceRoot>true</addCompileSourceRoot>
                        <configOptions>
                            <configOptions>
                                <apiPackage>${base.package}.services</apiPackage>
                                <invokerPackage>${base.package}.client</invokerPackage>
                                <addCompileSourceRoot>true</addCompileSourceRoot>
                                <sourceFolder>src</sourceFolder>
                                <dateLibrary>java8</dateLibrary>
                                <serializableModel>true</serializableModel>
                            </configOptions>
                            <sourceFolder>.</sourceFolder>
                        </configOptions>
                    </configuration>
                </execution>
                <execution>
                    <id>generate-web-ui-doc</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <language>html</language>
                        <inputSpec>${project.basedir}/src/main/resources/schema.yaml</inputSpec>
                        <output>src/gen/web</output>
                        <modelPackage>${base.package}.api.model</modelPackage>
                        <apiPackage>${base.package}.services</apiPackage>
                        <invokerPackage>${base.package}.client</invokerPackage>
                        <addCompileSourceRoot>true</addCompileSourceRoot>
                        <configOptions>
                            <configOptions>
                                <apiPackage>${base.package}.services</apiPackage>
                                <invokerPackage>${base.package}.client</invokerPackage>
                                <addCompileSourceRoot>true</addCompileSourceRoot>
                                <sourceFolder>src</sourceFolder>
                                <dateLibrary>java8</dateLibrary>
                                <serializableModel>true</serializableModel>
                            </configOptions>
                            <sourceFolder>.</sourceFolder>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>clean-additional-generated-files</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                    <configuration>
                        <excludeDefaultDirectories>true</excludeDefaultDirectories>
                        <filesets>
                            <fileset>
                                <directory>${codegen.rest.dir}</directory>
                                <includes>
                                    <include>**/*Controller.java</include>
                                    <include>**/invoker/*</include>
                                    <include>*.*</include>
                                    <include>io/**</include>
                                    <include>src/**</include>
                                    <include>.swagger-codegen/**</include>
                                    <include>**/ApiOriginFilter.java</include>
                                    <include>**/ApiException.java</include>
                                    <include>**/ApiResponseMessage.java</include>
                                    <include>**/NotFoundException.java</include>
                                </includes>
                                <followSymlinks>false</followSymlinks>
                            </fileset>
                        </filesets>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- Add cleaned directory with relevant Java files to the current project source paths -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>${codegen.rest.dir}</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- Compile generated code -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>6</source>
                <target>6</target>
            </configuration>
        </plugin>
        <!--Generate api-docs page-->
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <attachClasses>false</attachClasses>
                <webResources>
                    <resource>
                        <directory>target</directory>
                        <includes>
                            <include>${api.json.file}</include>
                        </includes>
                    </resource>
                    <resource>
                        <directory>target</directory>
                        <includes>
                            <include>internal.json</include>
                        </includes>
                    </resource>
                    <resource>
                        <directory>target/classes</directory>
                        <includes>
                            <include>META-INF/**/*</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>

schema.yaml:

swagger: "2.0"

信息:   版本:1.0.0   标题:简单API   description:学习如何编写OpenAPI规范的简单API

方案:   -http 主机:localhost basePath:/

路径:   /人:     得到:       摘要:得到一些人       描述:返回包含所有人员的列表。该列表支持分页。       参数:        -$ ref:“#/参数/ pageSize”        -$ ref:“#/参数/ pageNumber”       回应:         200:           描述:人员清单           模式:             $ ref:“#/ definitions /人”         500:           $ ref:“#/ responses / Standard500ErrorResponse”     帖子:       摘要:创建一个人       描述:将新人员添加到人员列表。       参数:         -姓名:人           在:身体           描述:创建人。           模式:             $ ref:“#/ definitions /人”       回应:         204:           描述:成功创建的人。         400:           描述:无法创建人员。         500:           $ ref:“#/ responses / Standard500ErrorResponse”

/人/ {用户名}:     参数:       -$ ref:“#/参数/用户名”     得到:       摘要:招人       描述:返回一个人作为其用户名。       回应:         200:           描述:一个人           模式:             $ ref:“#/ definitions /人”         404:           $ ref:“#/ responses / PersonDoesNotExistResponse”         500:           $ ref:“#/ responses / Standard500ErrorResponse”     删除:       摘要:删除一个人       描述:删除通过用户名标识的个人       回应:         204:           描述:此人已成功删除。         404:           $ ref:“#/ responses / PersonDoesNotExistResponse”         500:           $ ref:“#/ responses / Standard500ErrorResponse”

/人/ {用户名} /朋友:     参数:       -$ ref:“#/参数/用户名”     得到:       摘要:结识一个人的朋友       描述:返回包含所有人员的列表。该列表支持分页。       参数:        -$ ref:“#/参数/ pageSize”        -$ ref:“#/参数/ pageNumber”       回应:         200:           描述:一个人的朋友清单           模式:             $ ref:“#/ definitions /人”         404:           $ ref:“#/ responses / PersonDoesNotExistResponse”         500:           $ ref:“#/ responses / Standard500ErrorResponse”

定义:   人:     需要:       - 用户名     特性:       名字:         类型:字符串       姓:         类型:字符串       用户名:         类型:字符串   人数:     类型:数组     项目:       $ ref:“#/ definitions /人”   错误:     需要:       -代码       - 信息     特性:       码:         类型:字符串       信息:         类型:字符串

回应:   Standard500ErrorResponse:     描述:发生意外错误。     模式:       $ ref:“#/ definitions /错误”   PersonDoesNotExistResponse:     描述:人不存在。

参数:   用户名:     名称:用户名     在:路径     必填:true     描述:此人的用户名     类型:字符串   页面大小:     名称:pageSize     在:查询     描述:返回人数     类型:整数   pageNumber:     名称:pageNumber     在:查询     说明:页码     类型:整数

0 个答案:

没有答案