【问题标题】:Invalid mapping pattern detected: /**/swagger-ui/**检测到无效的映射模式:/**/swagger-ui/**
【发布时间】:2021-11-15 20:55:48
【问题描述】:

我正在将 springdoc-openapi-ui 用于 Spring Boot API 文档并面临以下问题 -

我已经添加了所有必要的配置如下 -

  1. maven 依赖 -
<dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.5.2</version>
</dependency>
  1. 这是主文件 -
package com.abc.tl;
    
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@OpenAPIDefinition
public class TLApplication {

    public static void main(String[] args) {

        SpringApplication.run(TLApplication.class, args);

    }
}


使用java版本-11,不确定问题出在哪里,项目无法运行。

【问题讨论】:

  • 你有没有试过直接通过属性文件指定swagger路径:springdoc.swagger-ui.path=/swagger-ui.html
  • @VadimB - 是的,我也试过了,但没有运气

标签: java spring-boot swagger swagger-ui springdoc-openapi-ui


【解决方案1】:

似乎PathPatternParser 不允许** 出现在中间,并且会拒绝这样的模式(查看更多详细信息:https://github.com/spring-projects/spring-boot/issues/21694)。

看起来这里的代码在路径模式的中间插入了一个**

    uiRootPath.append(ALL_PATTERN);
    registry.addResourceHandler(uiRootPath + SWAGGER_UI_PATTERN)

完整代码

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    StringBuilder uiRootPath = new StringBuilder();
    if (swaggerPath.contains(DEFAULT_PATH_SEPARATOR))
        uiRootPath.append(swaggerPath, 0, swaggerPath.lastIndexOf(DEFAULT_PATH_SEPARATOR));
    if (actuatorProvider.isPresent() && actuatorProvider.get().isUseManagementPort())
        uiRootPath.append(actuatorProvider.get().getBasePath());

    uiRootPath.append(ALL_PATTERN);
    registry.addResourceHandler(uiRootPath + SWAGGER_UI_PATTERN)
            .addResourceLocations(CLASSPATH_RESOURCE_LOCATION + DEFAULT_WEB_JARS_PREFIX_URL + DEFAULT_PATH_SEPARATOR)
            .resourceChain(false)
            .addTransformer(swaggerIndexTransformer);
}

【讨论】:

  • 我需要在哪里添加这段代码?
  • @BabitaBisht 看看你如何配置你的网络配置文件
  • 我是 Spring Boot 新手,在哪里可以找到这个 Web 配置文件?
【解决方案2】:

在您的代码中某处(我猜是在安全配置中)您配置了/**/swagger-ui/**。最近(Spring 5.3)这些路径配置现在由PathPatternParser 分析(取代了旧的AntPathMatcher,它允许像这样的路径)。

按照建议将spring.mvc.pathpattern.matching-strategy=ant_path_matcher 添加到您的application.properties 或将您的路径配置更改为"/webjars/swagger-ui/**" 之类的内容。

【讨论】:

  • 我尝试添加 spring.mvc.pathpattern.matching-strategy=ant_path_matcher 但没有成功
  • 你试过第二种方法了吗?将路径配置更改为 "/webjars/swagger-ui/**"
【解决方案3】:

通过以下依赖,解决了这个问题。

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.0</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.5.12</version>
</dependency>

【讨论】:

    【解决方案4】:

    以上项目有效 spring.mvc.pathpattern.matching-strategy=ant_path_matcher

    但是,它应该是 ant-path-matcher - 破折号而不是下划线

    【讨论】:

      【解决方案5】:

      我在我的应用程序 application.properties 中添加了对我有用的以下行。

      spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
      

      【讨论】:

        猜你喜欢
        • 2022-01-21
        • 2022-07-05
        • 1970-01-01
        • 1970-01-01
        • 2017-09-18
        • 2016-12-09
        • 2018-07-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多