当前位置: 首页 > news >正文

Springboot3.0.0+集成SpringDoc并配置knife4j的UI

环境:JDK17,Springboot3+,springdoc2+,knife4j 4+

Springdoc本身也是集成了Swagger3,而knife4j美化了Swagger3的UI

Knife4j官网:

快速开始 | Knife4j

Springdoc官网

OpenAPI 3 Library for spring-boot

1.pom配置

由于此knife4j内依赖了SpringDoc,因此不用另外引入springdoc的依赖

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

springdoc依赖(无需引入),亲测引入也不会冲突

        <dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.2.0</version></dependency>

2.application.yml配置

springdoc:swagger-ui:path: /swagger-ui.htmltags-sorter: alphaoperations-sorter: alphaenabled: trueapi-docs:path: /v3/api-docsenabled: truegroup-configs:group: platformpaths-to-match: /**packages-to-scan: com.license4j.licenseknife4j:enable: truesetting:language: zh_cn

3.代码配置

addInterceptors主要放开了文档的路径访问

addResourceHandlers主要设置了接口文档静态资源路径

addResourceHandlers配置很重要,不配置会导致接口文档404,后台也会报异常

No mapping for GET /doc.html

import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import java.util.ArrayList;
import java.util.List;@Configuration
public class WebMvcRegistrationsConfig extends WebMvcConfigurationSupport {@Resourceprivate LicenseInterceptor licenseInterceptor;public WebMvcRegistrationsConfig(LicenseInterceptor licenseInterceptor) {this.licenseInterceptor = licenseInterceptor;}/*** 拦截器配置* @param registry*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 无需拦截的接口集合List<String> ignorePath = new ArrayList<>();// swaggerignorePath.add("/swagger-resources/**");ignorePath.add("/doc.html");ignorePath.add("/v3/**");ignorePath.add("/webjars/**");ignorePath.add("/springdoc/**");ignorePath.add("/static/**");ignorePath.add("/templates/**");ignorePath.add("/error");ignorePath.add("/cipher/check");ignorePath.add("/manager/login");ignorePath.add("/swagger-ui.html");//先拦截认证,再拦截授权registry.addInterceptor(licenseInterceptor).addPathPatterns("/**").excludePathPatterns(ignorePath);}/*** 静态资源配置* @param registry*/@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}
}

swagger基本配置

import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class SwaggerConfig {private Info info(){return new Info().title("API接口文档").description("").version("v1.0.0");}@Beanpublic OpenAPI springShopOpenAPI() {return new OpenAPI().info(info()).externalDocs(externalDocumentation());}
}

4.访问路径 ip+端口+doc.html

http://www.lryc.cn/news/135201.html

相关文章:

  • 电脑运行缓慢?4个方法,加速电脑运行!
  • 3.Docker 搭建 MySQL8.0
  • Mybatis的SqlSource SqlNode BoundSql
  • html动态爱心代码【二】(附源码)
  • 【Rust】Rust学习 第十六章无畏并发
  • 系统报错mfc100u.dll丢失的解决方法(完美解决dll问题)
  • docker compose的用法
  • Linux: 使用 ssh 连接其他服务器
  • [.NET/WPF] CommunityToolkit.Mvvm 异步指令
  • 热烈祝贺汇隆成功入选航天系统采购供应商库
  • 2019年3月全国计算机等级考试真题(C语言二级)
  • MySQL 游标
  • ElasticSearch 7.4学习记录(DSL语法)
  • 全志orangepi-zero2驱动编写2,控制电平高低
  • 软考高级系统架构设计师系列之:论文典型试题写作要点和写作素材总结系列文章四
  • 06.利用Redis实现点赞功能
  • 【React】生命周期和钩子函数
  • 无涯教程-TensorFlow - 优化器
  • 基于AQS+双向链表实现队列先进先出
  • 无涯教程-Perl - time函数
  • CUDA Bug<三>当__global__函数出现里面所有输出的数组都随机赋值了
  • 甜椒叶病害识别(Python代码,pyTorch框架,深度卷积网络模型,很容易替换为其它模型,带有GUI识别界面)
  • Python爬虫——scrapy_日志信息以及日志级别
  • 微信小程序 echarts 画多个横向柱状图
  • 【二叉树】572. 另一棵树的子树
  • 220V转5V芯片三脚芯片-AH8652
  • windows系统丢失mfc120u.dll的解决方法
  • css 实现电梯导航
  • 【Spring Boot】Spring Retry减少1000 行代码讲解
  • 【数据结构OJ题】相交链表