如何将Java Swagger Annotation转换为Swagger json模式?

时间:2016-02-22 05:16:43

标签: java documentation swagger swagger-ui springfox

如何从包含Swagger特定注释的java文件生成JSON文件,以便Swagger-UI可以读取它。

1 个答案:

答案 0 :(得分:2)

正如您所说“我不想手动执行”,请使用Swagger Maven插件:

<plugin>
    <groupId>com.github.kongchen</groupId>
    <artifactId>swagger-maven-plugin</artifactId>
    <version>3.1.4</version>
    <configuration>
        <apiSources>
            <apiSource>
                <springmvc>true / false</springmvc>
                <locations>
                    <location>com.yourpackage.something</location>
                </locations>
                <host>yourhost.com</host>
                <basePath>/some/path</basePath>
                <info>
                    <title>Your Project Title</title>
                    <version>${project.version}</version>
                    <description>Some nice stuff</description>
                    <termsOfService>...</termsOfService>
                    <contact>
                        <email>someone@somewhere.com</email>
                        <name>Your Name</name>
                        <url>www.where.to.find.you</url>
                    </contact>
                </info>
                <swaggerDirectory>path/to/swagger/output</swaggerDirectory>
                <outputFormats>json</outputFormats>
            </apiSource>
        </apiSources>
    </configuration>
    <executions>
        <execution>
            <phase>compile</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

然后您可以执行mvn compile并生成swagger.json文件。