在CentOS上生成Swagger文檔通常涉及以下幾個步驟:
sudo yum install java-1.8.0-openjdk-devel
sudo yum install maven
<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>
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;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("cn.weijishu.server.api.rest"))
.paths(PathSelectors.any())
.build();
}
}
生成Swagger文檔:使用Swagger提供的注解在代碼中標記API,然后運行項目。Swagger會自動生成文檔。
訪問Swagger UI:啟動項目后,可以通過瀏覽器訪問Swagger UI頁面,查看和測試生成的API文檔。通??梢酝ㄟ^訪問http://localhost:8080/swagger-ui.html
來查看Swagger UI。