问题
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 see in the screenshot, the swagger-fox url backing the UI is example.com/api . NOTE: I get a standard Spring Whitelabel Error Page
when I navigate to: https://localhost:9600/api/v2/api-docs/ . I suspect this is the root of the problem? I see no errors that Spring didn't load springfox-swagger2
and so I don't know why that isn't working.
My config looks something like this (and I have tried all sorts of variations of this config, from searching the net for advice):
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = {"com.company.project"})
public class SwaggerConfig
{
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.cloud")))
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.data.rest.webmvc")))
.paths(PathSelectors.any())
.build();
}
}
And
<!-- to generate /swagger-ui.html -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
NOTE: Interestingly, when I try version 2.6.0, I don't get the modal popup but my Swagger UI shows 0 api content. So, i know that modal must be fairly new?
If there is not enough info here, leave me a comment.
回答1:
I was able to resolve the issue by adding SpringBootApplication with annotation - as suggested by Mark :
@EnableSwagger2
回答2:
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.
回答3:
It turns out that the solution to my problem was to move the @EnableSwagger2
annotation into the main Configuration class, along with the Docket Bean.
I had created a separate class in a sub-package with the Docket Bean and I was expecting it to get scanned by Spring and load the bean. Perhaps I failed to annotate that docket class with @Component
, or maybe the the problem was something else, but I got it working.
回答4:
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);
}
}
回答5:
For Spring 2 use @SpringBootApplication annotation and extends your class application from SpringBootServletInitializer then override the method SpringApplicationBuilder
@SpringBootApplication
public class TestApp extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(TestApp.class);
}
public static void main(String[] args) {
SpringApplication.run(TestApp.class, args);
}
}
回答6:
in my case i had this line in my WebSecurityConfig.java:
.antMatchers("swagger-ui.html").permitAll()
and when i added this one:
.antMatchers("/swagger-resources/**").permitAll()
my problem resolved.
回答7:
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.
回答8:
I am using spring boot.
In my case I made mistake in package name. in my project the base package name is
org.liferayasif.documents
so my configuration file should be inside
org.liferayasif.documents.config
config -> u can give any name.
but by mistake I put in different package name give below
org.liferayasif.config
once I put the configuration file inside proper package name in my case
org.liferayasif.documents.config
then it worked properly.
回答9:
I needed to add anonymous access to resources in Spring Security method: protected void configure(HttpSecurity http)
.antMatchers("/api/**", "/swagger-ui.html", "/webjars/**", "/v2/**", "/swagger-resources/**").anonymous()
And then it starts working.
来源:https://stackoverflow.com/questions/47425048/why-does-springfox-swagger2-ui-tell-me-unable-to-infer-base-url