Why does springfox-swagger2 UI tell me "Unable to infer base url."
Asked Answered
L

20

47

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.

enter image description here

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.

Lyn answered 22/11, 2017 at 0:38 Comment(2)
Put a breakpoint at springfox.documentation.swagger2.web.Swagger2Controller#getDocumentation and debug to find out why you are getting an error while trying to access https://localhost:9600/api/v2/api-docs/Shopkeeper
check your versions of springfox-swagger2 and springfox-swagger-ui. both versions should be same. I had this problem due to different versionsEqui
L
13

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.

Lyn answered 2/2, 2018 at 17:54 Comment(2)
for me worked adding @ComponentScan to my SwaggerConfig classCabinetmaker
Worked out for me adding @EnableSwagger2 annotation for my main config class. This actually happened for me in spring boot webapp.Acidify
F
54

I was able to resolve the issue by adding SpringBootApplication with annotation - as suggested by Mark :

@EnableSwagger2
Faubion answered 1/2, 2018 at 14:24 Comment(3)
I don't know how I missed to add it. after spending so much of time. it worked like a charm !!.Polythene
I had this annotation in the SwaggerConfig class but the "Unable to infer base URL" error persisted. When I moved this annotation to my @SpringBootApplication class, the problem went away.Danndanna
Does this still work with springfox swagger 3.0.0? I have the exact same issue and this fix did not do anything.Chammy
P
29

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.

Parboil answered 18/9, 2018 at 12:39 Comment(5)
how to add thisGanny
@Ganny : @Override public void configure(WebSecurity web) throws Exception {web.ignoring().mvcMatchers("/swagger-ui.html/**", "/configuration/**","/swagger-resources/**", "/v2/api-docs","/webjars/**");}Reggy
I am not using Spring Security, same problem.Coeternal
If 'not allowing anonymous' were the issue, seems like the error would different, like a permissions type of error.Lyn
Was the exact issue I had with Spring Security. Just add .antMatchers("/swagger-ui/*", "/swagger-ui.html", "/webjars/**", "/v2/**", "/swagger-resources/**").permitAll() to the WebSecurityConfiguration.configure HttpSecurity function, amongst the rest of your code withinDionne
L
13

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.

Lyn answered 2/2, 2018 at 17:54 Comment(2)
for me worked adding @ComponentScan to my SwaggerConfig classCabinetmaker
Worked out for me adding @EnableSwagger2 annotation for my main config class. This actually happened for me in spring boot webapp.Acidify
H
6

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);
    }

}
Headliner answered 24/6, 2020 at 19:40 Comment(1)
Yes, I believe this is the simplest resolution although there are other interesting ideas in this thread.Lyn
W
5

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.

Weitzel answered 12/11, 2019 at 15:0 Comment(0)
P
3

This has worked for me.

@Override
    public void configure(WebSecurity web) throws Exception {
         web
        .ignoring()
        .antMatchers(
                "/swagger-resources/**",
                "/swagger-ui.html",
                "/v2/api-docs",
                "/webjars/**");
    }

Make sure about the root path.

Patricio answered 18/1, 2021 at 11:43 Comment(0)
M
2

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);
   }
}
Malta answered 1/12, 2017 at 7:0 Comment(1)
... or mark spring-boot application with annotation: @EnableSwagger2Dory
S
2

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);
    }
}
Samellasameness answered 9/11, 2018 at 15:47 Comment(0)
H
2

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.

Hymanhymen answered 6/7, 2019 at 11:31 Comment(1)
This is because Spring scans the classpath and part of what Spring scans by default is either a 'config' folder in classpath root or a 'config' package. This makes sense. Whatever you do to fix this problem, just make sure Spring scanning picks up the swagger bean. I personally wouldn't solve it this way since people who don't understand the 'config' folder scanning will not truly understand your code.Lyn
V
1

It's 2024 and I ran into this problem still. I tried reading all the internet but hit a lot of dead ends until I actually bumped into a solution that works. Apparently, Spring Boot 3 needs a different library than Spring Boot 2 to be able to use Swagger 3. Let’s set it up in a new project and see how to use the most basic features.The solution was to add this dependency into the pom.xml file and boom, it worked.

<dependency>
   <groupId>org.springdoc</groupId>
   <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
   <version>2.0.2</version>
</dependency>

You can refer to the full article here.

Vories answered 8/2 at 19:12 Comment(0)
R
0

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.

Rudman answered 17/5, 2019 at 8:19 Comment(0)
H
0

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.

Hedve answered 19/8, 2019 at 19:20 Comment(0)
E
0

OAHH, for me it was something new. I had 2 different versions for springfox-swagger2 and springfox-swagger-ui

Before:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        **<version>2.6.1</version>**
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        **<version>2.9.2</version>**
    </dependency>

After:

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        **<version>2.9.2</version>**
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        **<version>2.9.2</version>**
    </dependency>

This solved my problem.

Equi answered 2/1, 2020 at 13:3 Comment(0)
F
0

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

Folderol answered 3/4, 2020 at 21:26 Comment(0)
G
0

this page localhost:9000/sawgger-ui with needs other external resources like swagger-ressources /v2/api-docs ... this resources is denyed so you mustto permit them or ignoring them

inspect your page to see it witch ressource needs to igniore or permit

Gennagennaro answered 28/6, 2022 at 14:10 Comment(0)
S
0

I haved same issue and spend few hours to resolved:

I use springfox version 3.0.0

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>

with springboot 2.3.10:

   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.10.RELEASE</version>

and spring security.

In my case I added paths to permitAll section in spring security configuration

 .antMatchers("  "/swagger-resources/**",
                    "/swagger-ui.html",
                    "/v2/api-docs",
                    "/webjars/**" ,
                    "/swagger-ui/**")
                .permitAll()
Substratosphere answered 3/11, 2022 at 13:9 Comment(0)
V
0

With this annotation it works for newer versions:

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

Via answered 10/3, 2023 at 14:50 Comment(0)
B
0

pay attention if you're using SPRING BOOT 3 you have to add dependency of springdoc-openapi-starter-webmvc-ui

<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
  <version>2.0.3</version>
</dependency>
Ballew answered 16/8, 2023 at 11:24 Comment(0)
I
0

maybe you can try this one, it's better to use swagger version below 2.7.0. I found that Springboot Application need annotaion @EnableSwagger2 .

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.6.1</version>
    </dependency>
Im answered 31/10, 2023 at 6:59 Comment(0)
P
-4

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

Playpen answered 15/7, 2020 at 17:18 Comment(1)
have you try Java 1.2 ?Mcghee

© 2022 - 2024 — McMap. All rights reserved.