首页 > 其他分享 >Swagger与SpringBoot冲突

Swagger与SpringBoot冲突

时间:2023-10-06 12:45:13浏览次数:38  
标签:java SpringBoot spring jar 29 springframework 冲突 context Swagger

SpringBoot版本2.7.15,Swagger版本2.9.2

集成Swagger时报错,报错信息如下

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182) ~[spring-context-5.3.29.jar:5.3.29]
	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.29.jar:5.3.29]
	at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:357) ~[spring-context-5.3.29.jar:5.3.29]
	at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_382]
	at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:156) ~[spring-context-5.3.29.jar:5.3.29]
	at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:124) ~[spring-context-5.3.29.jar:5.3.29]
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:938) ~[spring-context-5.3.29.jar:5.3.29]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.29.jar:5.3.29]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.15.jar:2.7.15]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) [spring-boot-2.7.15.jar:2.7.15]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) [spring-boot-2.7.15.jar:2.7.15]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.7.15.jar:2.7.15]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.7.15.jar:2.7.15]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.7.15.jar:2.7.15]
	at com.tobie.springbootdemo.SpringBootDemoApplication.main(SpringBootDemoApplication.java:23) [classes/:na]
Caused by: java.lang.NullPointerException: null
	at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:112) ~[springfox-spi-2.9.2.jar:null]
	at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:109) ~[springfox-spi-2.9.2.jar:null]
	at com.google.common.collect.ComparatorOrdering.compare(ComparatorOrdering.java:37) ~[guava-20.0.jar:na]
	at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~[na:1.8.0_382]
	at java.util.TimSort.sort(TimSort.java:220) ~[na:1.8.0_382]
	at java.util.Arrays.sort(Arrays.java:1438) ~[na:1.8.0_382]
	at com.google.common.collect.Ordering.sortedCopy(Ordering.java:855) ~[guava-20.0.jar:na]
	at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:57) ~[springfox-spring-web-2.9.2.jar:null]
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:138) ~[springfox-spring-web-2.9.2.jar:null]
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:135) ~[springfox-spring-web-2.9.2.jar:null]
	at com.google.common.collect.Iterators$7.transform(Iterators.java:750) ~[guava-20.0.jar:na]
	at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~[guava-20.0.jar:na]
	at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~[guava-20.0.jar:na]
	at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:52) ~[guava-20.0.jar:na]
	at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:50) ~[guava-20.0.jar:na]
	at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:249) ~[guava-20.0.jar:na]
	at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:209) ~[guava-20.0.jar:na]
	at com.google.common.collect.FluentIterable.toList(FluentIterable.java:614) ~[guava-20.0.jar:na]
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.defaultContextBuilder(DocumentationPluginsBootstrapper.java:111) ~[springfox-spring-web-2.9.2.jar:null]
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.buildContext(DocumentationPluginsBootstrapper.java:96) ~[springfox-spring-web-2.9.2.jar:null]
	at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167) ~[springfox-spring-web-2.9.2.jar:null]
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:179) ~[spring-context-5.3.29.jar:5.3.29]
	... 14 common frames omitted
 
 
Process finished with exit code 1

解决方案

网络上的解决方案如下,更改SpringBoot配置

spring:
  mvc:
   pathmatch:
     matching-strategy: ANT_PATH_MATCHER

但并没有解决问题,经过排查是Actuator的冲突,故提供以下两种解决方法:

方案一:删除Actuator依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

方案二:在Swagger配置类中手动更改Actuator相关信息

/**
     * 解决Swagger与SpringBoot 2.6.x适配问题
     *
     * @param webEndpointsSupplier webEndpointsSupplier
     * @param servletEndpointsSupplier servletEndpointsSupplier
     * @param controllerEndpointsSupplier controllerEndpointsSupplier
     * @param endpointMediaTypes endpointMediaTypes
     * @param corsProperties corsProperties
     * @param webEndpointProperties webEndpointProperties
     * @param environment environment
     * @return {@link WebMvcEndpointHandlerMapping}
     */
    @Bean
    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
        List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
        Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
        allEndpoints.addAll(webEndpoints);
        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
        String basePath = webEndpointProperties.getBasePath();
        EndpointMapping endpointMapping = new EndpointMapping(basePath);
        boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
    }
 
    private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
        return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
    }

标签:java,SpringBoot,spring,jar,29,springframework,冲突,context,Swagger
From: https://www.cnblogs.com/tobieance/p/17744440.html

相关文章

  • Springboot整合Redis
    一、Redis简介简单来说Redis就是一个使用C语言开发的数据库,不过与传统数据库不同的是Redis的数据是存在内存中的,也就是它是内存数据库,所以读写速度非常快,因此Redis被广泛应用于缓存方向。另外,Redis除了做缓存之外,Redis也经常用来做分布式锁,甚至是消息队列。Redis......
  • 基于Java+SpringBoot+Vue的微信小程序实现民族节日及民间故事推广系统
    一、前言......
  • 【SpringBoot】多环境配置和启动
    环境分类,可以分为本地环境、测试环境、生产环境等,通过对不同环境配置内容,来实现对不同环境做不同的事情。SpringBoot项目,通过application-xxx.yml添加不同的后缀来区分配置文件,启动时候通过后缀启动即可。application.yml:公共配置application-xxx.yml:不同环境的差异配......
  • springboot开发过程的一些细节
    注解:格式要求@DateTimeFormat(pattern=“yyyy-MM-DD”)@Pathvariable用来绑定动态请求参数@RequestBody用来接收前端传来的动态请求参数,一般post请求,对象接收。 在Controller层中返回值参数要与需求文档的参数相同。依赖:pagehelper依赖,实现分页更能跟便捷......
  • SpringBoot vue云办公系统
    SpringBootvue云办公系统系统功能云办公系统登录员工资料管理:搜索员工添加编辑删除员工导入导出excel薪资管理:工资账套管理添加编辑删除工资账套员工账套设置系统管理:基础信息设置部门管理职位管理职称管理权限组管理操作员管理开发环境和技术开发语言:Ja......
  • springboot+Uniapp+redis开发的AI医疗智能导诊系统源码
    AI+医疗的智能导诊系统源码 自主版权 支持二开一、什么是智能导诊系统?智能导诊系统是一种基于人工智能和大数据技术开发的医疗辅助软件,它能够通过对患者的症状、病史等信息进行计算分析,快速推荐科室和医生。通过简单的描述自身症状,系统即可找到最适合的科室,实现线上高效挂号,线下......
  • Springboot3
    Java17以上1.依赖<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.0.0</version><relativePath/></parent>2.新特性2.1JakartaEE......
  • Swagger2与Knife4j集成(防踩坑)
    Swagger2与Knife4j1集成Springboot1.1依赖<!--https://mvnrepository.com/artifact/io.springfox/springfox-swagger2--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger......
  • 搭建Springboot+Vue+Element的简单系统流程
    今天研究了一下如何把Springboot+Mybatis和Vue+Element结合起来使用详细写一篇博客来记录一下流程吧,因为途中发现了很多的问题首先,创建Springboot项目,惯例添加依赖<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="htt......
  • springboot静态资源加载问题:能加载到文件,但是文件没有内容。拦截器的问题。
    在使用springboot+thymeleaf的时候发现了这样的情况:加载到的js和css文件都没有内容。但是在项目中是正常的文件。尝试配置了许多东西之后发现是拦截器的问题。1、在实现了WebMvcConfigurer接口的配置类中先重写addResourceHandlers方法。@OverridepublicvoidaddResourceHa......