在CentOS上启动Swagger UI,通常需要先确保你的应用已经集成了Swagger。以下是一个基本的步骤指南,假设你已经有一个Spring Boot应用,并且已经添加了Swagger依赖。
如果你使用的是Maven,可以在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>
如果你使用的是Gradle,可以在build.gradle中添加以下依赖:
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
创建一个配置类来配置Swagger:
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("com.example.demo")) // 替换为你的包名
.paths(PathSelectors.any())
.build();
}
}
确保你的Spring Boot应用已经启动。你可以使用以下命令来启动应用:
./mvnw spring-boot:run
或者如果你使用的是Gradle:
./gradlew bootRun
一旦你的应用启动,你可以通过浏览器访问Swagger UI。默认情况下,Swagger UI的地址是:
http://localhost:8080/swagger-ui.html
如果你的应用运行在不同的端口上,请相应地调整URL。
通过以上步骤,你应该能够在CentOS上成功启动并访问Swagger UI。