springboot集成swagger3.0
创始人
2024-02-29 10:55:43
0

springboot集成swagger3.0

不同swagger版本访问路径

swagger版本访问路径
3.0.0之前http://127.0.0.1:8080/swagger-ui.html
3.0.0之后http://127.0.0.1:8080/swagger-ui/index.html

swagger不同版本启动类配置注解

swagger版本启动类配置注解
3.0.0之前@EnableSwagger2
3.0.0之后@EnableOpenApi

引入依赖

        io.springfoxspringfox-boot-starter3.0.0

yml配置

启动时如果出现以下报错Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException,需要在yml添加如下配置

spring:mvc:path-match:matching-strategy: ant_path_matcher

swagger配置类

swagger2.0和3.0指定的DocumentationType是不同的,2.0是SWAGGER_2,3.0是OAS_30;
Spring Boot 6.x 与Swagger 3.0.0 不兼容问题,会出现报错Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException,解决该问题方案如下:

@Configuration
public class SwaggerConfig {//访问http://127.0.0.1:1003/swagger-ui/index.html可以看到API文档@Beanpublic Docket api() {return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("test").description("test模块").version("1.0").license("转载请保留原文作者@Maruko").licenseUrl("https://blog.csdn.net/qq_35705176/article/details/127994334?spm=1001.2014.3001.5502").termsOfServiceUrl("").build();}/*** 增加如下配置可解决Spring Boot 6.x 与Swagger 3.0.0 不兼容问题**/@Beanpublic WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,ServletEndpointsSupplier servletEndpointsSupplier,ControllerEndpointsSupplier controllerEndpointsSupplier,EndpointMediaTypes endpointMediaTypes,CorsEndpointProperties corsProperties,WebEndpointProperties webEndpointProperties,Environment environment) {List> allEndpoints = new ArrayList();Collection webEndpoints = webEndpointsSupplier.getEndpoints();allEndpoints.addAll(webEndpoints);allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());String basePath = webEndpointProperties.getBasePath();EndpointMapping endpointMapping = new EndpointMapping(basePath);boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping);}private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));}}

碰到问题

在集成swagger3.0时,碰到一个问题,Request body参数示例为空,后面发现是因为添加了懒加载的原因,需要去掉该配置

spring:application:name: producer-develop
#  main:
#    lazy-initialization: true

相关内容

热门资讯

修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
【前端】‘??‘与‘||‘有什... 0 问题 经常写const data = res.data.a ?? ''或者const d...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
牛客计算器的改良(Python... 文章目录1.题目描述2.输入描述:3.输出描述:4.示例15.分析6.代码7.结语 链接࿱...
正大杯|市调大赛|2023备赛... 关键信息 同时随着精细化养宠趋势的深入,宠物消费类目日渐丰富。 本报告通过 Niuco...
QGIS下载在线地图(Goog... 前言 国内外有很多在线地图下载软件,但功能单一,基本上只能下载数据&#x...