【SpringBoot】Swagger&Knif4j接口文档集成

打印 上一主题 下一主题

主题 903|帖子 903|积分 2709

目录

序:接口文档

​        在开发过程中,接口文档是非常重要的一环,在 Spring Boot 中,我们可以通过集成第三方来实现接口文档的自动生成。
​        通过注解来描述接口,然后根据这些注解自动生成接口文档,它不仅方便开发者查看和理解接口的功能和参数,还能帮助前后端开发协同工作,提高开发效率。
​        常用的接口文档,有Swagger和Knife4j,推荐Knife4j 。

  • 作用

    • 方便前后端开发对接
    • 方便沉淀和维护
    • 支持在线调试、在线测试
    • 可以导出接口文档

1 Swagger

Swagger 是一个 RESTful 接口文档的规范和工具集,它的目标是统一 RESTful 接口文档的格式和规范。
1.1 基本信息

1.2 接入步骤


  • 根据maven中央仓库,引入包
    根据SpringBoot选择对应的版本,我的是SpringBoot版本是2.7.15

    • Swagger后端包
      1. [/code]
      2. [/list]     io.springfox    springfox-swagger2    2.9.2  ```
      3. [list]
      4. [*]Swagger前端包
      5. [code]<dependency>
      6.     <groupId>io.springfox</groupId>
      7.     <artifactId>springfox-swagger-ui</artifactId>
      8.     <version>2.9.2</version>
      9. </dependency>
      复制代码

  • 创建配置类
    新建SwaggerConfig.java 文件
    1. package com.leovany.usercenter.config;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import springfox.documentation.builders.ApiInfoBuilder;
    5. import springfox.documentation.builders.PathSelectors;
    6. import springfox.documentation.builders.RequestHandlerSelectors;
    7. import springfox.documentation.service.ApiInfo;
    8. import springfox.documentation.service.Contact;
    9. import springfox.documentation.spi.DocumentationType;
    10. import springfox.documentation.spring.web.plugins.Docket;
    11. import springfox.documentation.swagger2.annotations.EnableSwagger2;
    12. @Configuration
    13. @EnableSwagger2
    14. public class SwaggerConfig {
    15.     @Bean
    16.     public Docket productApi() {
    17.         return new Docket(DocumentationType.SWAGGER_2)
    18.                 .apiInfo(apiInfo())
    19.                 .select()
    20.                 // 这里一定要标注你控制器的位置
    21.                 .apis(RequestHandlerSelectors.basePackage("com.leovany.usercenter.controller"))
    22.                 .paths(PathSelectors.any())
    23.                 .build();
    24.     }
    25.     /**
    26.      * api 信息
    27.      *
    28.      * @return
    29.      */
    30.     private ApiInfo apiInfo() {
    31.         return new ApiInfoBuilder()
    32.                 .title("用户中心")
    33.                 .description("用户中心接口文档")
    34.                 .termsOfServiceUrl("https://github.com/leovany")
    35.                 .contact(new Contact("leovany", "https://github.com/leovany", "xxx@qq.com"))
    36.                 .version("v1.0.0")
    37.                 .build();
    38.     }
    39. }
    复制代码
  • 配置路径匹配策略

    • 如果 springboot version >= 2.6,需要添加如下配置
      1. spring:
      2.         mvc:
      3.                 pathmatch:
      4.             matching-strategy: ANT_PATH_MATCHER
      复制代码
    • 原因
      Spring MVC 的路径匹配策略是 ant-path-matcher,而 Spring Boot 2.6.x版本的默认匹配策略是 path-pattern-matcher,这就导致启动时报错,错误内容信息:
      1. Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
      2. ...
      复制代码

  • 启动项目,输入地址
    地址:http://localhost:8080/swagger-ui.html

2 Knife4j

knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案
2.1 基本信息

2.2 接入步骤


  • 根据maven中央仓库,引入包
    根据SpringBoot选择对应的版本,我的是SpringBoot版本是2.7.15
    1. <dependency>
    2.     <groupId>com.github.xiaoymin</groupId>
    3.     <artifactId>knife4j-spring-boot-starter</artifactId>
    4.     <version>3.0.3</version>
    5. </dependency>
    复制代码
  • 创建配置类
    新建Knife4jConfig.java 文件
    1. package com.leovany.usercenter.config;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.context.annotation.Profile;
    5. import springfox.documentation.builders.ApiInfoBuilder;
    6. import springfox.documentation.builders.PathSelectors;
    7. import springfox.documentation.builders.RequestHandlerSelectors;
    8. import springfox.documentation.service.ApiInfo;
    9. import springfox.documentation.service.Contact;
    10. import springfox.documentation.spi.DocumentationType;
    11. import springfox.documentation.spring.web.plugins.Docket;
    12. import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
    13. /**
    14. * 自定义 Knife4j 接口文档的配置
    15. */
    16. @Configuration
    17. @EnableSwagger2
    18. public class Knife4jConfig {
    19.     @Bean(value = "defaultApi2")
    20.     public Docket defaultApi2() {
    21.         return new Docket(DocumentationType.SWAGGER_2)
    22.                 .apiInfo(apiInfo())
    23.                 .select()
    24.                 // 项目controller的位置
    25.                 .apis(RequestHandlerSelectors.basePackage("com.leovany.usercenter.controller"))
    26.                 .paths(PathSelectors.any())
    27.                 .build();
    28.     }
    29.     /**
    30.      * api 信息
    31.      *
    32.      * @return
    33.      */
    34.     private ApiInfo apiInfo() {
    35.         return new ApiInfoBuilder()
    36.                 .title("用户中心")
    37.                 .description("用户中心接口文档")
    38.                 .termsOfServiceUrl("https://github.com/leovany")
    39.                 .contact(new Contact("leovany", "https://github.com/leovany", "xxx@qq.com"))
    40.                 .version("1.0.0")
    41.                 .build();
    42.     }
    43. }
    复制代码
  • 配置路径匹配策略

    • 如果 springboot version >= 2.6,需要添加如下配置
      1. spring:
      2.         mvc:
      3.                 pathmatch:
      4.             matching-strategy: ANT_PATH_MATCHER
      复制代码
    • 原因
      Spring MVC 的路径匹配策略是 ant-path-matcher,而 Spring Boot 2.6.x版本的默认匹配策略是 path-pattern-matcher,这就导致启动时报错,错误内容信息:
      1. Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
      2. ...
      复制代码

  • 配置是否屏蔽文档接口
    在文件application.yml配置,生产环境注意防止暴露接口文档(production设置为true)
    1. knife4j:
    2.   # 开启增强配置
    3.   enable: true
    4.   # 是否屏蔽接口文件(true=屏蔽,false=不屏蔽)
    5.   production: false
    复制代码
  • 启动项目,输入地址
    地址:http://localhost:8080/doc.html

本文由博客一文多发平台 OpenWrite 发布!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

铁佛

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表