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

springboot-2.3.3.RELEASE升级2.7.16,swagger2.9.2升级3.0.0过程

一、pom文件版本修改

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.16</version><relativePath/>
</parent>

如果用到了“spring-boot-starter-web”,“spring-boot-starter-validation”,“spring-boot-devtools”,“spring-boot-starter-test”,“spring-boot-test”。。。这些依赖也统一配置为2.7.X。

二、升级swagger相关依赖

<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>3.0.0</version><!-- 项目里没用到这个google依赖的话 这段可以删掉 --><exclusions><exclusion><groupId>com.google.guava</groupId><artifactId>guava</artifactId></exclusion></exclusions>
</dependency>
<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>3.0.0</version>
</dependency>
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>3.0.3</version>
</dependency><dependency><groupId>io.swagger</groupId><artifactId>swagger-annotations</artifactId><version>1.5.21</version>
</dependency>
<dependency><groupId>io.swagger</groupId><artifactId>swagger-models</artifactId><version>1.5.21</version>
</dependency>

三、swaggerConfig配置

springfoxHandlerProviderBeanPostProcessor方法是为了解决升级3.0.0后控制台报错“Failed to start bean 'documentationPluginsBootstrapper';”的问题,亲测有用。

package com.wyzz.global.config;import com.wyzz.model.constant.SwaggerVersionConstant;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;@Configuration
@EnableOpenApi
public class SwaggerConfig {/** 定义api接口包的扫描路径 */private static  final String SWAGGER_SCAN_PACKAGE = "com.xxx.controller";/** 版本信息 */private static final String API_VERSION = "1.0.0";@Beanpublic BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {return new BeanPostProcessor() {@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {customizeSpringfoxHandlerMappings(getHandlerMappings(bean));}return bean;}private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {List<T> copy = mappings.stream().filter(mapping -> mapping.getPatternParser() == null).collect(Collectors.toList());mappings.clear();mappings.addAll(copy);}@SuppressWarnings("unchecked")private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {try {Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");field.setAccessible(true);return (List<RequestMappingInfoHandlerMapping>) field.get(bean);} catch (IllegalArgumentException | IllegalAccessException e) {throw new IllegalStateException(e);}}};}@Beanpublic Docket createApi(){return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_PACKAGE)).paths(PathSelectors.any()).build();}private ApiInfo apiInfo(){return new ApiInfoBuilder().title("Server").description("API接口文档").version(API_VERSION).termsOfServiceUrl("http://localhost:9090/v3").build();}//按版本分组@Beanpublic Docket v100(){return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("v1.0.0").select().apis(input -> {ApiVersion apiVersion = input.getHandlerMethod().getMethodAnnotation(ApiVersion.class);return apiVersion != null && Arrays.asList(apiVersion.group()).contains("v1.0.0");}).paths(PathSelectors.any()).build();}
}

http://www.dtcms.com/a/274965.html

相关文章:

  • 猿人学js逆向比赛第一届第十九题
  • 大数据在UI前端的应用深化:用户行为数据的跨渠道整合分析
  • MinIO配置项速查表【五】
  • CentOS 安装 Redis 简明指南
  • linux中cmake编译项目
  • 深度学习14(循环神经网络)
  • Cocos游戏开发中,检测两个物体碰撞,并实现物理反弹逻辑
  • JAVA——选择结构、循环结构、随机数、嵌套循环、数组(一维、二维)
  • 亚古数据:澳大利亚公司的ABN和ACN号码是什么?
  • PyInstaller打包完整指南1
  • Java语言基础
  • 从硬件层面上限制电脑用户只能上网访问特定的网址
  • 知识就是力量——STM32(低功耗芯片方向)
  • ROS系统如何接管工业机械臂?
  • U2Fusion: A Unified UnsupervisedImage Fusion Network
  • 2025 js——面试题(7)——ajax相关
  • Linux自动化构建工具(一)
  • AI技术与大模型对比分析:发展趋势、应用场景及挑战
  • UI前端与数字孪生融合新领域:智慧环保的垃圾分类与回收系统
  • LLM场景下的强化学习【GRPO】
  • PCIE set_property问题
  • Java synchronized 锁机制深度解析与实战指南 - 银行转账案例
  • 深度学习超参数调优指南
  • Scrapy入门实战指南:从零开始打造高效爬虫系统
  • 每日算法刷题Day45 7.11:leetcode前缀和3道题,用时1h40min
  • 机器学习之线性回归(七)
  • 安全领域的 AI 采用:主要用例和需避免的错误
  • 基于k8s环境下pulsar高可用测试和扩缩容(上)
  • 基于k8s环境下pulsar高可用测试和扩缩容(下)
  • 线程通信与进程通信的区别笔记