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

Viv*_*nha 8 java documentation swagger swagger-ui springfox

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

Sim*_*erg 3

正如您所说“我不想手动执行此操作”,请使用 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>
Run Code Online (Sandbox Code Playgroud)

然后你就可以这样做mvn compile,它会生成swagger.json文件。