溫馨提示×

Linux中Swagger與Spring Boot如何集成

小樊
52
2025-02-23 06:55:16
欄目: 智能運維

在Linux環境中,將Swagger與Spring Boot集成可以幫助你創建、測試和文檔化你的RESTful API。以下是集成Swagger與Spring Boot的步驟:

1. 添加依賴

首先,在你的pom.xml(如果你使用Maven)或build.gradle(如果你使用Gradle)文件中添加Swagger和Springfox的依賴。

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'

2. 配置Swagger

創建一個配置類來配置Swagger。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.yourpackage.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

在這個配置中,RequestHandlerSelectors.basePackage("com.yourpackage.controller")指定了你的控制器所在的包。

3. 啟動Spring Boot應用

確保你的Spring Boot應用已經啟動。你可以使用以下命令來啟動你的應用:

./mvnw spring-boot:run

或者如果你使用的是Gradle:

./gradlew bootRun

4. 訪問Swagger UI

一旦你的應用啟動,你可以通過瀏覽器訪問Swagger UI。默認情況下,Swagger UI的地址是:

http://localhost:8080/swagger-ui.html

在這里,你可以查看和測試你的API。

5. 配置Swagger UI(可選)

你可以進一步自定義Swagger UI,例如添加認證、自定義樣式等。以下是一個簡單的示例,展示如何添加基本認證:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Collections;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yourpackage.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfo(
                "Your API Title",
                "Description of your API",
                "API TOS",
                "Terms of service",
                new Contact("Your Name", "www.example.com", "your.email@example.com"),
                "License of API", "API license URL", Collections.emptyList());
    }
}

通過這些步驟,你應該能夠在Linux環境中成功集成Swagger與Spring Boot,并使用Swagger UI來測試和文檔化你的API。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女