Spring MVC 如何生成swagger.json [副本]

oprakyz7  于 2023-02-09  发布在  Spring
关注(0)|答案(6)|浏览(397)
    • 此问题在此处已有答案**:

How to export swagger.json (or yaml)(7个答案)
三年前关闭了。
我正在使用java spring boot framework为我的项目创建REST api,我正在使用"springfox-swagger2 and springfox-swagger-ui"来生成swagger文档。我可以使用URL http://localhost:8080/swagger-ui.html查看我的文档。

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

qacovj5a1#

你可以在swagger-ui html页面中获取该url:

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

实际上你可以用chrome/firefox开发工具的网络功能得到所有的网址。

b4lqfgs4

b4lqfgs42#

如果使用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文档将在您的project /generated/swagger/目录中生成。请将其粘贴到以下地址:http://editor.swagger.io
并生成您想要的任何内容(您首选技术中的服务器端或客户端API)

3npbholx

3npbholx3#

我在这里有点晚了,但我刚刚发现,您可以打开您的浏览器控制台,找到GET请求的URL,该请求返回您的Swagger文档的JSON定义。以下技术在将我的APIMap到AWS API Gateway时对我很有效。
为此:
1.导航到您的Swagger docs端点
1.打开浏览器控制台
1.刷新页面
1.导航到网络选项卡并按XHR请求进行筛选
1.右键单击以?format=openapi结尾的XHR请求
1.现在,您只需将其复制并粘贴到一个新的JSON文件中即可!

vbopmzt1

vbopmzt14#

我是用一个小技巧做到的
我在家用控制器测试用例的末尾添加了以下代码

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>
xbp102n0

xbp102n05#

您应该可以在以下位置获取您的swagger.json
http://localhost:8080/api-docs
假设您没有像在宠物店示例应用程序中那样保留版本控制。在这种情况下,URL将是:
http://localhost:8080/v2/api-docs

fnatzsnv

fnatzsnv6#

要获取REST API的API JSON定义,如果swagger配置正确,您可以直接使用swagger/docs/v1,这意味着完整的URL将是,如果版本v1(或者只指定版本)
http://localhost:8080/swagger/docs/v1

相关问题