在 Linux 系統中,要導出 Swagger 文檔,你需要遵循以下步驟:
pom.xml
文件中添加以下依賴:<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
SwaggerConfig.java
,并添加以下代碼:import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
這段代碼會啟用 Swagger2 并允許所有請求和路徑。
啟動你的應用程序。確保 Swagger UI 已經成功啟動并可以在瀏覽器中訪問。通常,你可以通過訪問 http://localhost:8080/swagger-ui.html
來查看 Swagger UI。
導出 Swagger 文檔。在 Swagger UI 頁面中,點擊 “Authorize” 按鈕,然后點擊 “Download Swagger JSON” 按鈕。這將下載一個包含所有 API 信息的 JSON 文件。你也可以選擇導出為 YAML 格式,只需點擊 “Download Swagger YAML” 按鈕即可。
注意:這些步驟適用于基于 Java 的項目。如果你使用的是其他編程語言,導出 Swagger 文檔的方法可能會有所不同。