Springboot怎么整合Swagger

本文档介绍了如何在Spring Boot项目中集成Swagger2,创建详细的API文档,并通过Swagger-UI进行接口测试。首先,引入Swagger的相关依赖,然后配置SwaggerConfig启用Swagger2。完成配置后,访问http://localhost:9999/swagger-ui.html即可查看和测试API。在正式部署时,别忘了关闭Swagger以提高生产环境的安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导入依赖:

<!--整合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>

编写配置类SwaggerConfig

package com.fdw.springbootstudy.config;

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2//开启swagger2
public class SwaggerConfig {
}

打开页面:

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

 

配置多个API 分组

 

 实体类Model文档

实体类注释 

 

 接口方法文档

 

 

接口测试 

 

 正式发布的时候记得关闭Swagger