Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid mapping pattern detected: /**/swagger-ui/**

I am using springdoc-openapi-ui for spring boot API documentation and facing the following issue -

This is the error screen

I have added all the necessary configuration as follows -

  1. The maven dependency -
<dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.5.2</version>
</dependency>
  1. This is the main file -
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);

    }
}


Using java version - 11, Not sure where is the Issue, the project is not able to run.

like image 713
Babita Bisht Avatar asked Sep 22 '21 11:09

Babita Bisht


People also ask

How to fix invalid mapping pattern detected in Spring MVC?

Invalid mapping pattern detected: / /swagger-ui/ ^ No more pattern data allowed after {*...} or ** pattern element Fix this pattern in your application or switch to the legacy parser implementation with 'spring.mvc.pathmatch.matching-strategy=ant_path_matcher'.

What is Swagger UI?

Swagger UI. Swagger UI allows anyone — be it your development team or your end consumers — to visualize and interact with the API’s resources without having any of the implementation logic in place. It’s automatically generated from your OpenAPI (formerly known as Swagger) Specification, with the visual documentation making it easy...

How to use Swagger UI with Maven?

To use Swagger UI, we need to add an additional Maven dependency: Now we can test it in our browser by visiting: The result should look something like this: 5.2. Exploring Swagger Documentation

Can Swagger generate documentation for a spring REST API?

11. Conclusion In this article, we set up Swagger 2 to generate documentation for a Spring REST API. We also explored ways to visualize and customize Swagger's output. And finally, we looked at a simple OAuth configuration for Swagger. The full implementation of this tutorial can be found in the GitHub project.


Video Answer


6 Answers

With following dependencies, solved this issue.

 <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>
like image 156
Debmalya Jash Avatar answered Oct 19 '22 22:10

Debmalya Jash


I added below line in my app application.properties that worked for me.

spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
like image 7
Amit Avatar answered Oct 19 '22 22:10

Amit


The above item works spring.mvc.pathpattern.matching-strategy=ant_path_matcher

However, it should be ant-path-matcher - dashes not underscores

like image 4
user2286976 Avatar answered Oct 19 '22 21:10

user2286976


Seems The PathPatternParser doesn't allow ** to appear in the middle, and will reject such a pattern (see more details: https://github.com/spring-projects/spring-boot/issues/21694).

It looks like code here inserted a ** in the middle of the path pattern.

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

Full code

@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);
}
like image 2
Sachith Wickramaarachchi Avatar answered Oct 19 '22 21:10

Sachith Wickramaarachchi


Somewhere in your code (I would guess in the security configuration) you have configured /**/swagger-ui/**. Recently (Spring 5.3) these path configurations are now analyzed by PathPatternParser (that replaced the old AntPathMatcher that allowed paths just like these).

Either add spring.mvc.pathpattern.matching-strategy=ant_path_matcher to your application.properties as suggested or change the your path configuration to something like "/webjars/swagger-ui/**".

like image 1
João Dias Avatar answered Oct 19 '22 22:10

João Dias


In my case, I was using old versions of springdoc-openapi-ui springdoc-openapi-data-rest, which happened to be incompatible with the latest version of Spring Boot. Looking up for the most recent maven package on https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui and using that version, solved the problem.

like image 1
Haroldo_OK Avatar answered Oct 19 '22 21:10

Haroldo_OK