SpringBoot整合Knife4j接口文档

1. 在项目入口模块pom文件导入依赖

            <!-- knife4j(API 文档工具) -->
            <dependency>
                <groupId>com.github.xiaoymin</groupId>
                <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
                <version>${knife4j.version}</version>
            </dependency>

2. 在项目入口模块新建Knife4j配置类

配置类其实不是必须的,但是@EnableSwagger2WebMvc注解是必须的,它代表开启接口文档,如果你不配置例如以下的信息,可以直接在启动类添加该注解,但是我建议还是配置一下,起码看起来干净。

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/**
 * @Author: Desmond
 * @CreateTime: 2024-08-02
 * @Description: Knife4j配置类
 */

@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
    @Bean("webApi")
    public Docket createApiDoc() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInfo())
                // 分组名称
                .groupName("Web 前台接口")
                .select()
                // 这里指定 Controller 扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.desmond.web.controller"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    /**
     * 构建 API 信息
     * @return
     */
    private ApiInfo buildApiInfo() {
        return new ApiInfoBuilder()
                .title("xxx接口文档") // 标题
                .description("xxx是一款由 Spring Boot + Vue 3.2 开发的前后端分离项目") // 描述
                .termsOfServiceUrl("www.xxx.com") // API 服务条款
                .contact(new Contact("Desmond", "www.xxx.com", "xxx@.com")) // 联系人
                .version("1.0") // 版本号
                .build();
    }

3. 启动项目

如果控制台出现以上日志,说明配置成功

默认路径:http://localhost:8080/doc.html

如果你项目路径不一样,自行修改拼接/doc.html即可

页面展示如上,我个人认为比swagger2使用还更简单

4. 常用的几个注解

@Api 作用于controller

@ApiOperation 作用于接口

效果如下

@ApiOperation 的 notes参数还可以给接口进行注释

@ApiModel 作用于实体类

@ApiModelProperty 作用于类属性

 

5. 调试

点击发送即可对接口进行请求,如果你对响应类也写了相关注解,页面也会有相应的注释

6. 只在开发环境生效

可以在配置类使用@Profile("dev") ,这样Knife4j接口文档就只会在dev环境被计划

这个注解是SpringBoot的,这个注解可以让我们在不同环境使用不同的配置和参数。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot是一种便捷的框架,它可以快速地搭建Java应用程序,并且它对于集成其他组件和框架也十分方便。而Knife4j则是一种集成度很高的API文档工具,它可以将接口文档在Swagger的基础上大幅度优化。在Spring Boot中使用Knife4j整合API文档也非常简单。 首先,我们需要在Spring Boot的项目中引入Knife4j依赖,可以在pom.xml文件中加入以下代码: ``` <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.2.7</version> </dependency> ``` 这样Knife4j就会被自动集成到Spring Boot的应用中。 接下来,我们需要在Controller方法上增加注解,并且配置一些信息才能生成接口文档。 ``` @GetMapping("/hello") @ApiOperation(value = "示例API接口", notes = "这是一个示例API接口") @ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "用户名", required = true, dataType = "String", paramType = "header") }) public String hello(@RequestHeader String name){ return "Hello, " + name + "!"; } ``` 其中@GetMapping是Spring Boot的注解,用于标记这是一个GET请求。@ApiOperation和@ApiImplicitParams则是Knife4j的注解,它们分别用于注释方法和方法参数的信息。 最后,在启动Spring Boot应用后,访问http://localhost:8080/doc.html 就可以看到生成的接口文档了。这个文档列表会列出所有接口的URL、HTTP方法、请求参数、响应结果等信息,非常直观和有用。通过Knife4j可以使API文档生成更加高效、直观,方便开发者理解和调用接口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Desamond

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

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

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

打赏作者

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

抵扣说明:

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

余额充值