如何生成swagger.json

How to generate swagger.json

我正在使用 java spring 引导框架为我的项目创建 REST api,并且我正在使用 "springfox-swagger2 and springfox-swagger-ui" 生成 swagger 文档。我可以使用 URL http://localhost:8080/swagger-ui.html 查看我的文档。

如何创建或生成swagger.json / spec.json,文档不应该与此应用程序一起使用,我们正在使用单独的应用程序进行上市API 文档。

您可以通过 swagger-ui html 页面获得 url:

GET http://localhost:8080/v2/api-docs?group=App

实际上,您可以使用 chrome/firefox 开发工具网络功能获得所有 url。

如果您使用 Maven,则可以使用 swagger-maven-plugin

生成客户端和服务器端文档(yaml、json 和 html)

将此添加到您的 pom.xml:

.....
 <plugin>
                <groupId>com.github.kongchen</groupId>
                <artifactId>swagger-maven-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <apiSources>
                        <apiSource>
                            <springmvc>true</springmvc>
                            <locations>com.yourcontrollers.package.v1</locations>
                            <schemes>http,https</schemes>
                            <host>localhost:8080</host>
                            <basePath>/api-doc</basePath>
                            <info>
                                <title>Your API name</title>
                                <version>v1</version>
                                <description> description of your API</description>
                                <termsOfService>
                                    http://www.yourterms.com
                                </termsOfService>
                                <contact>
                                    <email>your-email@email.com</email>
                                    <name>Your Name</name>
                                    <url>http://www.contact-url.com</url>
                                </contact>
                                <license>
                                    <url>http://www.licence-url.com</url>
                                    <name>Commercial</name>
                                </license>
                            </info>
                            <!-- Support classpath or file absolute path here.
                            1) classpath e.g: "classpath:/markdown.hbs", "classpath:/templates/hello.html"
                            2) file e.g: "${basedir}/src/main/resources/markdown.hbs",
                                "${basedir}/src/main/resources/template/hello.html" -->
                            <templatePath>${basedir}/templates/strapdown.html.hbs</templatePath>
                            <outputPath>${basedir}/generated/document.html</outputPath>
                            <swaggerDirectory>generated/swagger-ui</swaggerDirectory>
                            <securityDefinitions>
                                <securityDefinition>
                                    <name>basicAuth</name>
                                    <type>basic</type>
                                </securityDefinition>
                            </securityDefinitions>
                        </apiSource>
                    </apiSources>
                </configuration>
            </plugin> ........

您可以在这个地址下载*.hbs模板: https://github.com/kongchen/swagger-maven-example

执行mvn swagger:generate JSon 文档将在您的项目 /generated/swagger/ 目录中生成。 把它放在这个地址上: http://editor.swagger.io

并生成您想要的任何内容(服务器端或客户端 API 在您的首选技术中)

我来晚了一点,但我刚刚发现您可以打开浏览器控制台并找到 URL 到 returns JSON 定义的 GET 请求对于您的 Swagger 文档。将我的 API 映射到 AWS API 网关时,以下技术对我有用。

为此:

  1. 导航到您的 Swagger 文档端点
  2. 打开浏览器控制台
  3. 刷新页面
  4. 导航到网络选项卡并按 XHR 请求过滤
  5. 右击以?format=openapi
  6. 结尾的XHR请求
  7. 您现在只需将其复制并粘贴到新的 JSON 文件中即可!

我用一个小技巧做到了

我在家庭控制器测试用例的末尾添加了以下代码

import org.springframework.boot.test.web.client.TestRestTemplate;

public class HomeControllerTest extends .... ...... {

@Autowired
private TestRestTemplate restTemplate;


@Test
public void testHome() throws Exception {
     //.......
     //... my home controller test code 
     //.....

    String swagger = this.restTemplate.getForObject("/v2/api-docs", String.class);

    this.writeFile("spec.json", swagger );
}

public void writeFile(String fileName, String content) {

    File theDir = new File("swagger");

    if (!theDir.exists()) {
        try{
            theDir.mkdir();
        } 
        catch(SecurityException se){ }        
    }

    BufferedWriter bw = null;
    FileWriter fw = null;
    try {
        fw = new FileWriter("swagger/"+fileName);
        bw = new BufferedWriter(fw);
        bw.write(content);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bw != null)
                bw.close();
            if (fw != null)
                fw.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

}
}

我不知道这是不是正确的方法,但它是有效的:)

依赖性

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.4.0</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.6.1</version>
    </dependency> 

您应该可以在

拿到您的 swagger.json

http://localhost:8080/api-docs

假设您没有保留宠物店示例应用程序中的版本控制。在这种情况下,URL 将是:

http://localhost:8080/v2/api-docs

获取 REST API 的 api json 定义,前提是正确配置了 swagger。您可以直接使用 swagger/docs/v1,这意味着完整的 url 将是,如果版本 v1(或仅指定版本)

http://localhost:8080/swagger/docs/v1