【问题标题】:How to generate swagger.json [duplicate]如何生成 swagger.json [重复]
【发布时间】:2017-01-23 14:16:08
【问题描述】:

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

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

【问题讨论】:

    标签: spring-mvc swagger swagger-ui swagger-2.0


    【解决方案1】:

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

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

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

    【讨论】:

    • 如何从这个 url 下载 swagger.json / spec.json 文件?
    • 在浏览器中输入完整的 URL.. 你会得到 JSON 作为响应。可以剪切和粘贴为json文件
    • 如何使用 java 做到这一点?
    • 你知道构建后是否将相应的文件放入jar中吗?这样我就不必启动应用程序并下载它...
    • @ka3ak 所以你可以试试这个问题的答案列表中MK-rou回答的(swagger-maven-plugin)[github.com/kongchen/swagger-maven-plugin],你可以参考这个答案。
    【解决方案2】:

    如果您使用 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 将在您的项目 /generated/swagger/ 目录中生成 JSON 文档。 把它放在这个地址上: http://editor.swagger.io

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

    【讨论】:

    • 这似乎有点不舒服,因为我已经用 Swagger2 注释记录了我的整个 API。
    • @tgr 插件完全支持对 swagger.json 进行 Swagger2 注释
    【解决方案3】:

    我用一个小技巧做到了这一点

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

    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】:

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

      为此:

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

      【讨论】:

      • + 你必须复制 XHR 请求的响应正文内容,其中 JSON 可用
      • 我怎样才能使用 java 完成所有这些工作??
      【解决方案5】:

      你应该可以在

      获得你的 swagger.json

      http://localhost:8080/api-docs

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

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

      【讨论】:

        【解决方案6】:

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

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

        【讨论】:

        • 工作得很好。感谢分享。
        • 这是一个简单而有用的提示。谢谢。
        • 谢谢大家@Hezron Naresh
        猜你喜欢
        • 1970-01-01
        • 2017-01-29
        • 2016-06-21
        • 2019-02-09
        • 2021-01-07
        • 2018-04-10
        • 1970-01-01
        • 2022-12-23
        • 1970-01-01
        相关资源
        最近更新 更多