在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>
注意:版本號應根據您的Spring Boot版本進行調整,以避免兼容性問題。
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.basePackage("com.example.yourproject")) // 這里寫你的Controller包路徑
.paths(PathSelectors.any())
.build();
}
}
這段代碼會啟用Swagger 2并允許所有請求和路徑。
http://localhost:8080/swagger-ui.html來查看Swagger UI。以上步驟適用于基于Java的Spring Boot項目。如果您使用的是其他編程語言,例如Python或Node.js,生成Swagger文檔的方法可能會有所不同。對于Python項目,可以使用Flask框架結合flask-swag或flasgger庫來生成Swagger文檔。對于Node.js項目,可以使用swagger-jsdoc和swagger-ui-express來配置和生成Swagger文檔。