Springboot2.6.x高版本與Swagger2版本沖突問(wèn)題解決方法
問(wèn)題:
Spring Boot 2.6.x版本引入依賴 springfox-boot-starter
(Swagger 3.0) 后,啟動(dòng)容器會(huì)報(bào)錯(cuò):
Failed to start bean ‘ documentationPluginsBootstrapper ‘ ; nested exception…
原因
Springfox 假設(shè) Spring MVC 的路徑匹配策略是 ant-path-matcher
,而 Spring Boot 2.6.x版本的默認(rèn)匹配策略是 path-pattern-matcher
,這就造成了上面的報(bào)錯(cuò)。
完整解決方案:
1. pom配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.6.4</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
2. 添加Bean
import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.stereotype.Component; import org.springframework.util.ReflectionUtils; import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider; import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; import java.lang.reflect.Field; import java.util.List; import java.util.stream.Collectors; @Component public class SwaggerBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) { List<RequestMappingInfoHandlerMapping> handlerMappings = getHandlerMappings(bean); customizeSpringfoxHandlerMappings(handlerMappings); } 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); } } }
3. swagger配置類(lèi)繼承 WebMvcConfigurationSupport
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class Swagger2Config extends WebMvcConfigurationSupport { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(ApiInfo.DEFAULT); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry. addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/") .resourceChain(false); } }
4. 訪問(wèn) http://{ip}:{port}/swagger-ui/index.html
當(dāng)然還是要感謝技術(shù)大佬,我只會(huì)匯總解決了治本的完整解決方法
到此這篇關(guān)于Springboot2.6.x高版本與Swagger2版本沖突問(wèn)題解決方法的文章就介紹到這了,更多相關(guān)Springboot2.6.x與Swagger2版本沖突內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
[Spring MVC] -簡(jiǎn)單表單提交實(shí)例
本篇文章主要介紹了[Spring MVC] -簡(jiǎn)單表單提交實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。2016-12-12SpringBoot使用Redis實(shí)現(xiàn)分布式鎖
這篇文章主要為大家詳細(xì)介紹了SpringBoot使用Redis實(shí)現(xiàn)分布式鎖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05springboot~ObjectMapper~dto到entity的自動(dòng)賦值
這篇文章主要介紹了springboot~ObjectMapper~dto到entity的自動(dòng)賦值,本文分三種情況給大家介紹,需要的朋友可以參考下2018-08-08springcloud整合gateway實(shí)現(xiàn)網(wǎng)關(guān)的示例代碼
本文主要介紹了springcloud整合gateway實(shí)現(xiàn)網(wǎng)關(guān)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Java利用Swagger2自動(dòng)生成對(duì)外接口的文檔
這篇文章主要介紹了Java利用Swagger2自動(dòng)生成對(duì)外接口的文檔,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06Java17和springboot3.0使用shiro報(bào)ClassNotFoundException的解決
本文主要介紹了Java17和springboot3.0使用shiro報(bào)ClassNotFoundException的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04