欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot2.6.x高版本與Swagger2版本沖突問(wèn)題解決方法

 更新時(shí)間:2022年04月13日 11:13:14   作者:cd4479163  
Spring Boot 2.6.x版本引入依賴?springfox-boot-starter?(Swagger?3.0) 后,啟動(dòng)容器會(huì)報(bào)錯(cuò),本文就介紹一下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í)例

    本篇文章主要介紹了[Spring MVC] -簡(jiǎn)單表單提交實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。
    2016-12-12
  • Java?Swing的層次結(jié)構(gòu)深入理解

    Java?Swing的層次結(jié)構(gòu)深入理解

    這篇文章主要介紹了Java?Swing的層次結(jié)構(gòu)深入理解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • SpringBoot使用Redis實(shí)現(xiàn)分布式鎖

    SpringBoot使用Redis實(shí)現(xiàn)分布式鎖

    這篇文章主要為大家詳細(xì)介紹了SpringBoot使用Redis實(shí)現(xiàn)分布式鎖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • springboot~ObjectMapper~dto到entity的自動(dòng)賦值

    springboot~ObjectMapper~dto到entity的自動(dòng)賦值

    這篇文章主要介紹了springboot~ObjectMapper~dto到entity的自動(dòng)賦值,本文分三種情況給大家介紹,需要的朋友可以參考下
    2018-08-08
  • java實(shí)現(xiàn)的正則工具類(lèi)

    java實(shí)現(xiàn)的正則工具類(lèi)

    這篇文章主要介紹了java實(shí)現(xiàn)的正則工具類(lèi),可用于針對(duì)電話號(hào)碼、郵箱、QQ號(hào)碼、QQ密碼、手機(jī)號(hào)的正則驗(yàn)證功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • springcloud整合gateway實(shí)現(xiàn)網(wǎng)關(guān)的示例代碼

    springcloud整合gateway實(shí)現(xiàn)網(wǎng)關(guān)的示例代碼

    本文主要介紹了springcloud整合gateway實(shí)現(xiàn)網(wǎng)關(guān)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • MyBatis查詢返回Map示例代碼

    MyBatis查詢返回Map示例代碼

    這篇文章主要給大家介紹了關(guān)于MyBatis查詢返回Map的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • Java中雙冒號(hào)::的作用舉例詳解

    Java中雙冒號(hào)::的作用舉例詳解

    這篇文章主要給大家介紹了關(guān)于Java中雙冒號(hào)::作用的相關(guān)資料,雙冒號(hào)(::)運(yùn)算符在Java?8中被用作方法引用(method?reference),方法引用是與lambda表達(dá)式相關(guān)的一個(gè)重要特性,需要的朋友可以參考下
    2023-11-11
  • Java利用Swagger2自動(dòng)生成對(duì)外接口的文檔

    Java利用Swagger2自動(dòng)生成對(duì)外接口的文檔

    這篇文章主要介紹了Java利用Swagger2自動(dòng)生成對(duì)外接口的文檔,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • Java17和springboot3.0使用shiro報(bào)ClassNotFoundException的解決

    Java17和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

最新評(píng)論