如何生成swagger.json

Bik*_*h M 27 spring-mvc swagger swagger-ui swagger-2.0

我正在使用java spring boot框架为我的项目创建REST api,我使用"springfox-swagger2和springfox-swagger-ui"来生成swagger文档.我可以使用url http:// localhost:8080/swagger-ui.html查看我的文档.

如何创建或生成swagger.json/spec.json,文档不应该与此应用程序一起使用我们使用单独的应用程序列出api文档

Lip*_*ang 18

您可以使用swagger-ui html页面获取网址:

在此输入图像描述

GET http://localhost:8080/v2/api-docs?group=App
Run Code Online (Sandbox Code Playgroud)

实际上你可以通过chrome/firefox开发工具网络功能获得所有网址.

  • 如何从该URL下载swagger.json / spec.json文件? (3认同)

小智 10

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

去做这个:

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


MK-*_*rou 9

如果使用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> ........
Run Code Online (Sandbox Code Playgroud)

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

执行mvn swagger:generate JSon文档将在您的项目/ generation / swagger /目录中生成。将其粘贴到此地址:http : //editor.swagger.io

并生成所需的内容(首选技术中的服务器端或客户端API)


Bik*_*h M 8

我用一个小技巧完成了这个

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

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();
        }

    }

}
}
Run Code Online (Sandbox Code Playgroud)

我不知道这是正确的方式但是它正在工作:)

依赖

    <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> 
Run Code Online (Sandbox Code Playgroud)


Nic*_*olò 5

您应该可以在以下位置获取swagger.json

http:// localhost:8080 / api-docs

假设您没有像pet store示例应用程序中那样保留版本。在这种情况下,URL为:

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