关于Swagger生成Api文档

本文档详细介绍了如何在Spring Boot项目中集成并配置Swagger2,包括添加依赖、配置文件设定、启动类标识以及如何使用@Api和ApiOperation等注解进行API文档的构建。通过配置basePackage,你可以指定扫描特定包下的控制器,以展示对应的API。此外,还展示了如何创建多个不同的API组,如adminApiInfo,用于区分不同权限的API。最后,通过访问http://localhost:8080/swagger-ui.html#/,可以查看和测试生成的Swagger UI界面。

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

pom导包

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
 

配置文件

@Configuartion      //表明配置文件注解

public class Swagger 2Config {

@Bean

       public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()


                //basePackage后面路径,为扫描路径,在项目中的控制层controller


                .apis(RequestHandlerSelectors.basePackage("com.fengmang.demo.controller"))
                .paths(PathSelectors.any())
                .build();
   }

private ApiInfo apiInfo(){

return new ApiInfoBuilder()

@Configuration
@EnableSwagger2
public class Swagger2Config {
    /**
     * 创建API应用
     * apiInfo() 增加API相关信息
     * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
     * 指定扫描的包路径来定义指定要建立API的目录。
     * @return
     */
    @Bean
    public Docket coreApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(adminApiInfo())
                .groupName("adminApi")
                .select()
                //只显示admin下面的路径
                .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
                .build();
    }

    private ApiInfo adminApiInfo(){
        return new ApiInfoBuilder()
                .title("标签")
                .description("用途描述")
                .version("1.0")
                .contact(new Contact("名字","路径","邮箱"))
                .build();
    }
}

启动类加

@EnableSwagger2  开启Swagger

 

@APi声名该controller啥玩意

@ApiOperation(给接口备注)

 @ApiModelPraperty(实体类上给字段标注的

启动模块

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

端口号自己用到的模块中xml文件里的来  

### 使用Swagger生成API文档的方法 #### 对于Go语言项目使用go-swagger工具 选择`go-swagger`作为生成Swagger文档的工具,主要因为其能够通过解析代码中的注释来自动构建详细的API描述文件[^1]。这种方式不仅减少了手动维护文档的工作量,还提高了文档与实际实现的一致性。 为了利用`go-swagger`创建API文档,在源码中适当位置添加特定格式的注释即可: ```go // Package main is the entry point of our application. package main import "net/http" // ServeHTTP responds to HTTP requests with a simple message. // swagger:route GET /hello helloWorld // // Say Hello World. // // Responses: // 200: okResponse func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello world")) } ``` 接着可以通过命令行运行如下指令来自动生成对应的JSON或YAML格式的Swagger定义文件: ```bash swag init -g ./main.go --output ./docs/swagger.json ``` 此过程会扫描指定包内的所有Go文件并提取其中符合Swagger规范的注释信息用于组装最终输出的文档结构。 #### 针对Java/Spring Boot应用集成SpringFox-Swagger库 对于采用Spring框架的应用程序来说,则更倾向于借助第三方库如SpringFox来简化这一流程[^2]。具体操作涉及修改项目的Maven配置文件(`pom.xml`)以加入必要的依赖项以及激活相应的功能开关: 在`pom.xml`内追加以下片段完成依赖声明: ```xml <dependencies> <!--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> </dependencies> ``` 随后需确保应用程序的主要启动类上标注了`@EnableSwagger2`以便启用Swagger特性支持: ```java @SpringBootApplication @EnableSwagger2 // 开启Swagger能力 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 上述设置完成后,随着服务端口开放访问,默认情况下就能直接浏览到由Swagger UI渲染出来的交互式API帮助页了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值