Swagger 2 Issue spring Boot
Asked Answered
C

7

9

I'm facing issue with Swagger Integration in Spring Boot. Have a look at the code and error snippet.

------------------POM--------------------

<properties>
    <java.version>1.8</java.version>
    <swagger.version>2.9.2</swagger.version>
</properties>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${swagger.version}</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${swagger.version}</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-bean-validators</artifactId>
    <version>${swagger.version}</version>
</dependency>

-----------------App class--------------

@SpringBootApplication
@EnableSwagger2
public class ProducerApplication {

  public static void main(String[] args) {
    SpringApplication.run(ServletPocProducerApplication.class, args);
  }
  
  @Bean 
  public Docket api() { 
    return new Docket(DocumentationType.SWAGGER_2)
      .select() 
      .apis(RequestHandlerSelectors.any())
      .paths(PathSelectors.any())
      .build();
  }
}

Stack Trace

org.springframework.context.ApplicationContextException: Failed to start bean 
'documentationPluginsBootstrapper'; nested exception is 
 java.lang.NullPointerException: Cannot invoke 
"org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.toString()" 
because the return value of 
"springfox.documentation.spi.service.contexts.Orderings.patternsCondition(springfox.docume 
ntation.RequestHandler)" is null
   at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.13.jar:5.3.13]

How do I fix this??

Cohberg answered 20/11, 2021 at 6:50 Comment(1)
S
23

I solved it by adding "spring.mvc.pathmatch.matching-strategy=ant-path-matcher" in application.properties.

Slantwise answered 20/12, 2021 at 0:51 Comment(2)
Thanks @Leandro Lima for the suggestion. It worked for me.Daric
I tried that, but it didn't fix the problem for meElseelset
C
11

For a long time I have tried to solve this problem and solution for this is:

a) adding this to application.properties:

spring.mvc.pathmatch.matching-strategy=ant-path-matcher 

b) adding this to application.yaml(or application.yml):

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
Cacilia answered 11/3, 2022 at 20:2 Comment(0)
F
3

I know this does not solve your problem directly, but consider moving to springdoc. Springfox is so buggy at this point that is a pain to use. I've moved to springdoc 2 years ago because of its Spring WebFlux support and I am very happy about it. Additionally, it also supports Kotlin Coroutines, which I am not sure Springfox does.

If you decide to migrate, springdoc even has a migration guide.

Flyn answered 20/11, 2021 at 12:18 Comment(2)
Works like a charm for me! Springdoc is also so much easier to set up!Elseelset
Indeed! A new major version is on the way.Tophus
D
2

Adding this "spring.mvc.pathmatch.matching-strategy=ant-path-matcher" to your application.properties file solves the problem. It's what i used and i saved me alot of trouble.

Dorisdorisa answered 9/3, 2022 at 15:57 Comment(0)
I
0

For the integration between spring-boot and swagger-ui, add the library to the list of your project dependencies (No additional configuration is needed):

     <dependency>
          <groupId>org.springdoc</groupId>
          <artifactId>springdoc-openapi-ui</artifactId>
          <version>1.5.12</version>
     </dependency>
  • The Swagger UI page will then be available at http://server:port/context-path/swagger-ui.html and the OpenAPI description will be available at the following url for json format: http://server:port/context-path/v3/api-docs

  • server: The server name or IP

  • port: The server port

  • context-path: The context path of the application

Indira answered 21/11, 2021 at 19:49 Comment(1)
Didn't work for meElseelset
O
0

Replace these in Pom.xml.

Change the parent version to 2.5.0

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

and add these:

<!-- https://mvnrepository.com/artifact/io.springfox/springfox- swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.5.0</version>
</dependency>
<dependency>
    <groupId> io.springfox </groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.5.0</version> 
</dependency>
Oliviero answered 20/3, 2023 at 7:50 Comment(1)
It was working finally thank youOliviero
T
-1

My suggestion is when you are using spring-boot then it is better to use spring boot dependency for swagger. So, spring-boot will take care of your default settings.

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>...</version>
</dependency>
Tenuous answered 20/12, 2021 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.