springboot2.6.x高版本与swagger2版本冲突问题解决方法-mile米乐体育

目录

  • 问题:
  • 原因
  • 完整mile米乐体育的解决方案:

问题:

spring boot 2.6.x版本引入依赖springfox-boot-starter(swagger3.0) 后,启动容器会报错:

failed to start bean ‘ documentationpluginsbootstrapper ‘ ; nested exception…

原因

springfox 假设 springmvc的路径匹配策略是ant-path-matcher,而 spring boot 2.6.x版本的默认匹配策略是path-pattern-matcher,这就造成了上面的报错。

完整mile米乐体育的解决方案:

1. pom配置

org.springframework.boot spring-boot-starter-web 2.6.4 io.springfox springfox-boot-starter 3.0.0

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 handlermappings = gethandlermappings(bean); customizespringfoxhandlermappings(handlermappings); } return bean; } private void customizespringfoxhandlermappings(list mappings) { list copy = mappings.stream() .filter(mapping -> mapping.getpatternparser() == null) .collect(collectors.tolist()); mappings.clear(); mappings.addall(copy); } @suppresswarnings("unchecked") private listhttp://www.cppcns.com gethandlermappings(object bean) { try { field field = reflectionutils.findfield(bean.getclass(), "handlermappings"); field.setaccessible(true); return (list) 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

当然还是要感谢技术大佬,我只会汇总解决了治本的完整解决方法

到此这篇关于springboot2.6.x高版本与swagger2版本冲突问题解决方法的文章就介绍到这了,更多相关springboot2.6.x与swagger2版本冲突内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

展开全文
内容来源于互联网和用户投稿,文章中一旦含有米乐app官网登录的联系方式务必识别真假,本站仅做信息展示不承担任何相关责任,如有侵权或涉及法律问题请联系米乐app官网登录删除

最新文章

网站地图