Swagger + Knife4j 接口文档的整合

这篇具有很好参考价值的文章主要介绍了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

Swagger + Knife4j 接口文档的整合,java

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

Swagger + Knife4j 接口文档的整合,java文章来源地址https://www.toymoban.com/news/detail-808447.html

到了这里,关于Swagger + Knife4j 接口文档的整合的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring Boot 2.6 以上整合 Swagger + Knife4j 报错

    这个问题主要出现在 Spring Boot 2.6 及以后,只要是 Spring Boot 2.6 引入的新 PathPatternParser 导致的。 两种解决办法 Path匹配策略切换回 ​​ant_path_matcher ​ 添加下面这个Bean的定义

    2024年01月17日
    浏览(41)
  • Spring Boot3整合knife4j(swagger3)

    目录 1.前置条件 2.导依赖 3.配置 已经初始化好一个spring boot项目且版本为3X,项目可正常启动。 作者版本为3.2.2 初始化教程: 新版idea创建spring boot项目-CSDN博客 https://blog.csdn.net/qq_62262918/article/details/135785412?spm=1001.2014.3001.5501 knife4j官网: Knife4j · 集Swagger2及OpenAPI3为一体的增强

    2024年01月23日
    浏览(31)
  • knife4j接口文档

    knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案,前身是swagger-bootstrap-ui,取名knife4j是希望它能像一把匕首一样小巧,轻量,并且功能强悍!其底层是对Springfox的封装,使用方式也和Springfox一致,只是对接口文档UI进行了优化。 核心功能 : 文档说明 :根据Swagger的规范说明

    2023年04月08日
    浏览(26)
  • SpringBoot3中Swagger整合knife4j和springdoc的配置说明

      springboot3开始javax包改成了jakarta,而swagger-oas等包中依然使用的是javax所以报错。另外springfox已经过时了,两年没更新了,并且不支持OpenAPI3 标准,而SpringBoot3只支持OpenAPI3规范,所以要迁移到springdoc Knife4J是一款基于Swagger快速生成API文档和调试平台的开源工具,它可以轻松地

    2024年02月04日
    浏览(37)
  • Spring Boot 集成 API 文档 - Swagger、Knife4J、Smart-Doc

    Swagger 作为 API 设计和文档的强大工具,是一个由专门的工具集合支持的框架,它在整个 API 的生命周期中发挥作用,从设计和文档,到测试和部署。通过提供可视化界面,Swagger 让开发人员和最终用户都能清晰地理解和操作 API。 使用建议:笔者建议优先考虑 Knife4J,它已经能

    2024年01月22日
    浏览(44)
  • 【SpringBoot笔记42】SpringBoot集成knife4j生成接口文档

    这篇文章,主要介绍SpringBoot如何集成knife4j及生成接口文档。 目录 一、knife4j接口文档生成器 1.1、接口文档工具介绍 1.2、引入依赖

    2024年02月05日
    浏览(24)
  • Springboot 2.7 集成 Swagger 增强版接口框架 Knife4j 4.3 + springdoc OpenApi 3.0

    Swagger 作为一款服务端接口文档自动生成框架,早已深入人心,并且在市场上得到了广泛的应用。然而,Swagger 3.0 也就是 OpenApi 3.0 规范发布之后便停止了更新维护,出道就是巅峰。Knife4j 作为 Swagger 的增强版,是对 Swagger UI 做了优化,同时还有很多增强的功能。伴随着 Swagge

    2024年02月08日
    浏览(31)
  • 【SpringBoot】Swagger和knife4j的使用

    springboot笔记集合: springboot笔记合计 没用的废话理论不多说,会用就完了 Swagger 是一种开源的API描述语言,就是描述API的, 同时Swagger还提供了一组工具(也叫Swagger),可以帮助开发人员自动生成API文档、测试API并与其他系统集成。 Knife4j是基于Swagge语言延伸的另一组api工具,简

    2024年02月10日
    浏览(25)
  • Spring Cloud Gateway + Knife4j 4.3 实现微服务网关聚合接口文档

    🚀 作者主页: 有来技术 🔥 开源项目: youlai-mall 🍃 vue3-element-admin 🍃 youlai-boot 🌺 仓库主页: Gitee 💫 Github 💫 GitCode 💖 欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请纠正! youlai-mall 开源微服务商城新版本基于 Spring Boot 3 和 Java 17,同时采用 Knife4j 4.3。与以前版本不同的是

    2024年02月08日
    浏览(30)
  • SpringBoot 整合knife4j

    Knife4j是一款基于Swagger 2的在线API文档框架 添加依赖 创建 Swagger 配置依赖 application.yml配置文件 响应参数 tips: http://127.0.0.1:8080/doc.html 这里端口,就是你运行项目的端口 springboot 中 knife4j的完整参数如下: 接口添加作者 添加作者有俩种方式 在方法上使用注解 @ApiOperationSupport

    2024年02月14日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包