SpringBoot2整合Knife4j 4.0

文章介绍了如何在SpringBoot项目中集成Knife4j以支持OpenAPI3规范,包括引入特定版本的依赖,配置springdoc-openapi和Knife4j的相关设置,以及项目启动后访问API文档的URL。此外,还提到了Knife4j的增强配置选项和文档参考。
摘要由CSDN通过智能技术生成

Knife4j官网地址

前置条件

  • springBoot版本大于2.4

引入依赖(以openapi3为例)

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

OpenAPI3规范Knife4j底层依赖了springdoc-openapi项目,需要项目中有该项目依赖

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <!--版本需和knife4j-openapi3-spring-boot-starter中依赖的版本相同-->
    <version>1.6.9</version>
</dependency>

配置项

# springdoc-openapi项目配置
springdoc:
  swagger-ui:
    path: /swagger-ui.html
    tags-sorter: alpha
    operations-sorter: alpha
  api-docs:
    path: /v3/api-docs
  group-configs:
    - group: 'default'
      paths-to-match: '/**'
      # 注意包路径分割符是点,直接在IDEA中copy的路径是斜杠会导致配置不生效  
      packages-to-scan: com.xiaominfo.knife4j.demo.web
# knife4j的增强配置,不需要增强可以不配
knife4j:
  enable: true
  setting:
    language: zh_cn

引入之后,其余的配置可完全参考springdoc-openapi

Knife4j更多增强配置明细,参考文档

使用

项目启动后访问:http://ip:port/doc.html

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
springboot2.7整合knife4j4.3是指在Springboot 2.7版本中使用Knife4j 4.3进行API文档的生成和管理。具体步骤如下: 1.在pom.xml文件中添加Knife4j的依赖: ``` <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>4.3.0</version> </dependency> ``` 2.在application.yml文件中进行配置: ``` spring: profiles: active: dev application: name: demo-knife4j-openapi3 servlet: multipart: max-file-size: 10MB max-request-size: 100MB resources: static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8 serialization: fail-on-empty-beans: false indent-output: true write-dates-as-timestamps: false write-null-map-values: false write-empty-json-arrays: false write-enums-using-to-string: true http: encoding: charset: UTF-8 enabled: true servlet: session: timeout: 30m datasource: url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource druid: initial-size: 5 min-idle: 5 max-active: 20 test-on-borrow: true validation-query: SELECT 1 FROM DUAL filters: stat,wall,log4j connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 jpa: show-sql: true hibernate: ddl-auto: update naming: physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl server: port: 8080 tomcat: uri-encoding: UTF-8 max-threads: 200 min-spare-threads: 10 max-http-header-size: 1048576 accesslog: enabled: true directory: logs prefix: access_log suffix: .log rotate: true pattern: common knife4j: title: demo-knife4j-openapi3 description: demo-knife4j-openapi3 version: 1.0.0 contact: name: demo-knife4j-openapi3 url: http://localhost:8080/doc.html email: demo-knife4j-openapi3@qq.com license: name: Apache License 2.0 url: http://www.apache.org/licenses/LICENSE-2.0.html terms-of-service-url: http://localhost:8080/doc.html host: localhost:8080 packages: com.example.demo ``` 3.在启动类上添加@EnableKnife4j注解: ``` @SpringBootApplication @EnableKnife4j public class DemoKnife4jOpenapi3Application { public static void main(String[] args) { SpringApplication.run(DemoKnife4jOpenapi3Application.class, args); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值