Swagger + Knife4j 接口文档的整合

Swagger 接口文档的整合:

  1. 引入依赖(Swagger 或 Knife4j)。
  2. 自定义 Swagger 配置类。
  3. 定义需要生成接口文档的代码位置(Controller)。
  4. 注意:线上环境不要把接口暴露出去!!!可以通过在 SwaggerConfig 配置文件开头加上 @Profile({“dev”, “test”}) 限定配置仅在部分环境开启。
  5. 启动接口文档。
  6. 可以通过在 controller 方法上添加 @Api、@ApiImplicitParam(name = “name”,value = “姓名”,required = true) @ApiOperation(value = “向客人问好”) 等注解来自定义生成的接口描述信息

Swagger

Swagger 官网

  1. 依赖引入
      <!-- 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>
  1. 创建 config 文件
package com.heo.matchmatebackend.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
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.EnableSwagger2;

/**
 * 自定义 Swagger 接口文档的配置
 */
@Configuration // 配置类
@EnableSwagger2 // 开启 swagger2 的自动配置
@Profile({"dev", "test"})   //版本控制访问
public class SwaggerConfig {
    @Bean(value = "defaultApi2")
    public Docket docket() {
        // 创建一个 swagger 的 bean 实例
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // 配置接口信息
                .select() // 设置扫描接口
                // 配置如何扫描接口
                .apis(RequestHandlerSelectors
                                //.any() // 扫描全部的接口,默认
                                //.none() // 全部不扫描
                                .basePackage("com.heo.matchmatebackend.controller") // 扫描指定包下的接口,最为常用
                        //.withClassAnnotation(RestController.class) // 扫描带有指定注解的类下所有接口
                        //.withMethodAnnotation(PostMapping.class) // 扫描带有只当注解的方法接口
                )
                .paths(PathSelectors
                                .any() // 满足条件的路径,该断言总为true
                        //.none() // 不满足条件的路径,该断言总为false(可用于生成环境屏蔽 swagger)
                        //.ant("/user/**") // 满足字符串表达式路径
                        //.regex("") // 符合正则的路径
                )
                .build();
    }

    /**
     * api 信息
     * @return
     */
    private ApiInfo apiInfo() {
        Contact contact = new Contact(
                "heo", // 作者姓名
                "https://blog.csdn.net/XiugongHao", // 作者网址
                "xxx@qq.com"); // 作者邮箱
        return new ApiInfoBuilder()
                .title("matchmate") // 标题
                .description("matchmate 接口文档") // 描述
                .termsOfServiceUrl("https://blog.csdn.net/XiugongHao") // 跳转连接
                .version("1.0") // 版本
                .contact(contact)
                .build();
    }
}

  1. yml 配置(如果 springboot version >= 2.6,需要添加如下配置 pathmatch)
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
  profiles:
    active: dev
  1. 最后运行启动。

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

在这里插入图片描述

Knife4j

Knife4j 官网

  1. 依赖引入。
        <!-- knife4j 接口文档 -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.7</version>
        </dependency>
  1. config 文件配置。
package com.heo.matchmatebackend.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
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.EnableSwagger2;

/**
 * 自定义 Swagger 接口文档的配置
 */
@Configuration // 配置类
@EnableSwagger2 // 开启 swagger2 的自动配置
@Profile({"dev", "test"})   //版本控制访问
public class SwaggerConfig {
    @Bean(value = "defaultApi2")
    public Docket docket() {
        // 创建一个 swagger 的 bean 实例
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                // 配置接口信息
                .select() // 设置扫描接口
                // 配置如何扫描接口
                .apis(RequestHandlerSelectors
                                //.any() // 扫描全部的接口,默认
                                //.none() // 全部不扫描
                                .basePackage("com.heo.matchmatebackend.controller") // 扫描指定包下的接口,最为常用
                        //.withClassAnnotation(RestController.class) // 扫描带有指定注解的类下所有接口
                        //.withMethodAnnotation(PostMapping.class) // 扫描带有只当注解的方法接口
                )
                .paths(PathSelectors
                                .any() // 满足条件的路径,该断言总为true
                        //.none() // 不满足条件的路径,该断言总为false(可用于生成环境屏蔽 swagger)
                        //.ant("/user/**") // 满足字符串表达式路径
                        //.regex("") // 符合正则的路径
                )
                .build();
    }

    /**
     * api 信息
     * @return
     */
    private ApiInfo apiInfo() {
        Contact contact = new Contact(
                "heo", // 作者姓名
                "https://blog.csdn.net/XiugongHao", // 作者网址
                "xxx@qq.com"); // 作者邮箱
        return new ApiInfoBuilder()
                .title("matchmate") // 标题
                .description("matchmate 接口文档") // 描述
                .termsOfServiceUrl("https://blog.csdn.net/XiugongHao") // 跳转连接
                .version("1.0") // 版本
                .contact(contact)
                .build();
    }
}

  1. yml 配置。
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
  profiles:
    active: dev
  1. 启动。

http://localhost:8080/api/doc.html#/home

在这里插入图片描述

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

小秀_heo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值