温馨提示×

SpringBoot中如何集成Swagger文档

小亿
82
2024-04-18 16:09:14
栏目: 编程语言

要在Spring Boot项目中集成Swagger文档,可以按照以下步骤操作:

  1. 添加Swagger依赖: 在项目的pom.xml文件中添加Swagger的依赖:
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
  1. 创建Swagger配置类: 在项目中创建一个Swagger配置类,用于配置Swagger的相关信息,例如文档标题、描述、版本等。示例代码如下:
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API Documentation")
                .description("Swagger documentation for API endpoints")
                .version("1.0")
                .build();
    }
}
  1. 启用Swagger: 在Spring Boot的启动类上使用@EnableSwagger2注解来启用Swagger:
@SpringBootApplication
@EnableSwagger2
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  1. 访问Swagger文档: 启动Spring Boot应用程序后,可以通过访问http://localhost:8080/swagger-ui/来查看生成的Swagger文档界面,其中包含了API的各个接口和相关信息。

通过以上步骤,就可以在Spring Boot项目中集成Swagger文档并查看API接口的详细信息。

0