温馨提示×

CentOS Swagger依赖怎么装

小樊
33
2025-12-25 13:11:24
栏目: 智能运维

在CentOS上安装Swagger依赖,通常需要以下几个步骤:

1. 安装Java

Swagger工具通常需要Java环境。你可以使用以下命令安装OpenJDK:

sudo yum install java-1.8.0-openjdk-devel

2. 安装Maven

Swagger工具通常使用Maven进行构建和管理依赖。你可以使用以下命令安装Maven:

sudo yum install maven

3. 安装Swagger工具

你可以使用Maven来下载和安装Swagger工具。以下是一个示例命令,用于下载Swagger UI:

mvn clean install -DskipTests

4. 配置Swagger

下载完成后,你可以将Swagger UI的静态文件部署到你的Web服务器上。例如,如果你使用的是Apache HTTP服务器,可以将Swagger UI的文件复制到你的Web服务器的根目录下。

5. 配置Spring Boot(如果使用Spring Boot)

如果你使用的是Spring Boot,可以在你的pom.xml文件中添加Swagger依赖:

<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应用程序中配置Swagger:

import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.springframework.context.annotation.Bean;
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.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;

@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();
    }

    @Bean
    public UiConfiguration uiConfig() {
        return UiConfigurationBuilder.builder()
                .deepLinking(true)
                .displayOperationId(false)
                .defaultModelsExpandDepth(1)
                .defaultModelExpandDepth(1)
                .defaultModelRendering(ModelRendering.EXAMPLE)
                .displayRequestDuration(false)
                .docExpansion(DocExpansion.NONE)
                .filter(false)
                .maxDisplayedTags(null)
                .operationsSorter(OperationsSorter.ALPHA)
                .showExtensions(false)
                .tagsSorter(TagsSorter.ALPHA)
                .supportedSubmitMethods(UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS)
                .validatorUrl(null)
                .build();
    }
}

6. 启动应用程序

启动你的Spring Boot应用程序,然后访问http://localhost:8080/swagger-ui.html即可看到Swagger UI界面。

通过以上步骤,你应该能够在CentOS上成功安装和配置Swagger依赖。

0