首页 文章

如何生成swagger.json

提问于
浏览
16

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

How can I crate or generate swagger.json / spec.json ,文档不应该与此应用程序一起使用我们使用单独的应用程序列出api文档

5 回答

  • 14

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

    enter image description here

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

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

  • 4

    我在这里有点晚了,但我发现你可以打开你的浏览器控制台并找到GET请求的URL,它返回你的Swagger文档的JSON定义 . 将API映射到AWS API Gateway时,以下技术对我有用 .

    去做这个:

    • 导航到您的Swagger docs endpoints

    • 打开浏览器控制台

    • 刷新页面

    • 导航到网络选项卡并按XHR请求过滤

    • 右键单击以 ?format=openapi 结尾的XHR请求

    • 您现在可以将其复制并粘贴到新的JSON文件中!

  • 1

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

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

    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>
    
  • 4

    如果您使用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:生成JSon文档将在您的project / generated / swagger /目录中生成 . 过去就这个地址:http://editor.swagger.io

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

  • 2

    你应该能够得到你的swagger.json

    http://localhost:8080/api-docs

    假设您没有像宠物商店示例应用程序那样保留版本控制 . 在这种情况下,URL将是:

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

相关问题