Knife4j--使用教程--配置 详细讲解

1 Knife4j

是基于springboot构建的一个文档生成工具,它可以让开发者为我们的应用生成API文档,目的是可以更加方便的基于API文档进行测试。

官方文档

2 Knife4j配置

本机springboot版本为2.7.6,因此选择OpenAPI2(Swagger)版本,底层依赖框架为Springfox,

Spring Boot 版本在2.4.0~3.0.0之间建议使用OpenAPI2(Swagger)。

2.1 添加依赖

在pom.xml文件中添加如下依赖:

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
    <version>4.2.0</version>
</dependency>

2.2 配置Swagger相关信息

工程目录下创建config.SwaggerConfig

package com.jasper.user_center.config;

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

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {
    
    //配置Swagger2的Docket的Bean实例
    @Bean(value = "defaultApi2")
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                // apiInfo():配置 API 的一些基本信息,比如:文档标题title,文档描述description,文档版本号version
                .apiInfo(apiInfo())
                // select():生成 API 文档的选择器,用于指定要生成哪些 API 文档
                .select()
                // apis():指定要生成哪个包下的 API 文档
               .apis(RequestHandlerSelectors.basePackage("com.jasper.user_center.controller"))
                // paths():指定要生成哪个 URL 匹配模式下的 API 文档。这里使用 PathSelectors.any(),表示生成所有的 API 文档。
                .paths(PathSelectors.any())
                .build();
    }
    //文档信息配置
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                // 文档标题
                .title("swagger_knife4j")
                // 文档描述信息
                .description("在线API文档")
                // 文档版本号
                .version("1.0")
                .build();
    }
}

启动SpringBoot项目后出现基本就代表配置成功了~

image-20230802222857727

2.3 查看生成的接口文档

在 SpringBoot 项目启动后,访问Knife4j的文档地址:http://ip:port/doc.html即可查看文档

本项目对应文档地址为:http://localhost:8080/api/doc.html

image-20230803124856043
finish~

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jasper_108

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值