Springboot2.6.x高版本與Swagger2版本沖突問題解決方法
問題:
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配置類繼承 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. 訪問 http://{ip}:{port}/swagger-ui/index.html

當(dāng)然還是要感謝技術(shù)大佬,我只會(huì)匯總解決了治本的完整解決方法
到此這篇關(guān)于Springboot2.6.x高版本與Swagger2版本沖突問題解決方法的文章就介紹到這了,更多相關(guān)Springboot2.6.x與Swagger2版本沖突內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java設(shè)計(jì)模式學(xué)習(xí)之代理模式
這篇文章主要為大家詳細(xì)介紹了java設(shè)計(jì)模式學(xué)習(xí)之代理模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Spring中使用AOP進(jìn)行事務(wù)管理實(shí)例
這篇文章主要介紹了Spring中使用AOP進(jìn)行事務(wù)管理實(shí)例,當(dāng)在Spring項(xiàng)目中涉及數(shù)據(jù)庫(kù)操作時(shí),事務(wù)管理是非常重要的,它可以確保數(shù)據(jù)庫(kù)操作的一致性和完整性,Spring提供了強(qiáng)大的事務(wù)管理功能,可以通過聲明式或編程式兩種方式進(jìn)行配置,需要的朋友可以參考下2023-09-09
java對(duì)圖片進(jìn)行壓縮和resize縮放的方法
本篇文章主要介紹了java對(duì)圖片進(jìn)行壓縮和resize調(diào)整的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
淺談Java中Unicode的編碼和實(shí)現(xiàn)
這篇文章向大家介紹了Java編程中Unicode編碼及實(shí)現(xiàn)的相關(guān)內(nèi)容,列舉了幾個(gè)字符不同表達(dá)式的比較,以及Unicode平面映射的知識(shí),具有一點(diǎn)點(diǎn)參考價(jià)值,需要的朋友可以了解下。2017-10-10
SpringBoot封裝自己的Starter的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot封裝自己的Starter的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04

