Why does springfox-swagger2 UI tell me “Unable to infer base url.”

前端 未结 13 866
醉话见心
醉话见心 2020-12-10 11:01

Why does springfox-swagger2 UI tell me Unable to infer base url. As far as I know, I am using a typical Swagger spring-boot configuration.

As you can

相关标签:
13条回答
  • 2020-12-10 11:24

    Add @EnableSwagger2 annotation in the main file of Springboot application

    @SpringBootApplication
    @EnableSwagger2
    public class SampleApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SampleApplication.class, args);
        }
    
    }
    
    0 讨论(0)
  • 2020-12-10 11:26

    In my case I am deploying our springboot(2.1.2) with swagger2(2.9.2) application as war file in tomcat8.5 and getting the same error.

    Later I was able to fix just by extending this class file AppConfig extends SpringBootServletInitializer

    My complete AppConfig file

    package org.mydomain
    
    import java.net.URL;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.domain.EntityScan;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @SpringBootApplication(scanBasePackages = "org.mydomain")
    @EntityScan({"org.mydomain.entity"})
    @EnableJpaRepositories({"org.mydomain.repository"})
    @EnableSwagger2
    @EnableScheduling
    public class AppConfig extends SpringBootServletInitializer {
    
      // We can optionally have spring boot way of starting main method
      public static void main(String[] args) {
         SpringApplication.run(AppConfig.class, args);
      }
    }
    

    This is the blog I referred how to deploy springboot application into tomcat

    Note: I have not configured swagger docket just used the default implementation.

    0 讨论(0)
  • 2020-12-10 11:30

    I had the same issue, but (maybe) I had a version missmatch in the pom.xml

    My project version is:

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

    Then, It started to work when I added the following dependency:

      <dependency>
            <groupId>org.springframework.plugin</groupId>
            <artifactId>spring-plugin-core</artifactId>
            <version>1.2.0.RELEASE</version>
        </dependency>
    

    Found at: https://github.com/springfox/springfox/issues/2932

    0 讨论(0)
  • 2020-12-10 11:34

    For me the problem was that Spring Security was not allowing access to some resources needed by swagger-ui. The solution was to allow anonymous access to the following paths:

    • /swagger-ui.html
    • /webjars/**
    • /v2/**
    • /swagger-resources/**

    In my case it is fine that these resources can be accessed without authentication.

    0 讨论(0)
  • 2020-12-10 11:35

    Just Extend your main class with SpringBootServletInitalizer. It Will Work fine. Take reference as bellow.

    public class TestApp extends SpringBootServletInitializer{
    
        public static void main(String[] args) {
    
           SpringApplication.run(TestApp.class, args);
       }
    }
    
    0 讨论(0)
  • 2020-12-10 11:36

    Check you Java Version first, it seems to not work with Version Java 11, try using Java 8 seems to be fine

    0 讨论(0)
提交回复
热议问题