Home > database >  Swagger 2 Issue spring Boot
Swagger 2 Issue spring Boot

Time:11-22

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??

CodePudding user response:

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.

CodePudding user response:

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

  • Related