question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to Infer base URL

See original GitHub issue

This might be one of the most common issues faced with SpringFox with plethora of answers. But I am unable to fix this issue even after following all the usual advice. Details:

I am using Spring Boot: v2.0.3.RELEASE and Spring Fox: v2.9.2

Spring Security Config:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
    private UserDetailsService userDetailsService;
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    private SecurityProperties securityProperties;
    private AppProperties AppProperties;

    public WebSecurityConfig(UserDetailsService userDetailsService,
                             BCryptPasswordEncoder bCryptPasswordEncoder,
                             SecurityProperties securityProperties,
                             AppProperties AppProperties) {
        this.userDetailsService = userDetailsService;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
        this.securityProperties = securityProperties;
        this.AppProperties = AppProperties;
    }

    @Configuration
    @Order(1)
    public class BasicAuthenticationConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .antMatcher("/admin/**")
                    .csrf().disable()
                    .httpBasic().and()
                    .authorizeRequests().anyRequest().hasRole("ADMIN").and()
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        }

        @Override
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
                    .withUser(AppProperties.getAdminUsername())
                    .password(bCryptPasswordEncoder.encode(AppProperties.getAdminPassword()))
                    .roles("ADMIN");
        }
    }

    @Configuration
    @Order(2)
    public class TokenAuthenticationConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.
                    cors().and().csrf().disable()
                    .authorizeRequests()
                    .antMatchers("/no-auth/**").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .addFilter(new JWTAuthenticationFilter(securityProperties, authenticationManager()))
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        }

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

        @Override
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
        }

        @Bean
        CorsConfigurationSource corsConfigurationSource() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            corsConfiguration.applyPermitDefaultValues();
            corsConfiguration.setAllowedOrigins(Collections.singletonList("*"));
            corsConfiguration.setAllowedMethods(Arrays.asList("GET", "POST", "DELETE"));
            corsConfiguration.setMaxAge(3600L);
            final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", corsConfiguration);
            return source;
        }
    }
}

Springfox Config:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Autowired
    private AppProperties AppProperties;

    @Bean
    public WebMvcConfigurer mvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("swagger-ui.html")
                        .addResourceLocations("classpath:/META-INF/resources/");

                registry.addResourceHandler("/webjars/**")
                        .addResourceLocations("classpath:/META-INF/resources/webjars/");
            }
        };
    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("foo-api")
                .apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                .produces(Collections.singleton("application/json"))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Foo API")
                .termsOfServiceUrl(AppProperties.getApiTosUrl())
                .version("1.0").build();
    }
}

Visiting: http://127.0.0.1:8080/swagger-ui.html

Displays:

Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:

Repeatedly clicking Ok in the popup doesn’t help.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
drakiulacommented, Dec 13, 2018

Spring Boot 2.1.1.RELEASE with spring fox 2.9.2, I am encountering the same issue, I will paste the log entries(DEBUG) when trying to load the http://localhost:8890/swagger-ui.html:

14:00:04.913 [2138] [XNIO-3 task-1] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /swagger-ui.html
14:00:08.073 [2138] [XNIO-3 task-2] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /webjars/springfox-swagger-ui/springfox.css
14:00:09.805 [2138] [XNIO-3 task-3] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /webjars/springfox-swagger-ui/swagger-ui.css
14:00:09.839 [2138] [XNIO-3 task-4] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /webjars/springfox-swagger-ui/swagger-ui-bundle.js
14:00:10.762 [2138] [XNIO-3 task-5] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /webjars/springfox-swagger-ui/springfox.js
14:00:11.774 [2138] [XNIO-3 task-6] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /webjars/springfox-swagger-ui/swagger-ui-standalone-preset.js
14:00:14.428 [2138] [XNIO-3 task-7] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /swagger-resources/configuration/ui
14:00:15.679 [2138] [XNIO-3 task-7] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping [getHandler] AbstractHandlerMapping.java:420) - Mapped to public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()

It seems the PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] fails to find the handler methods for those endpoints, still trying to make this work.

Now the output from another Spring Boot application (same versions as above), where the swagger ui is functioning:

14:09:01.688 [2155] [XNIO-3 task-1] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /swagger-resources/configuration/ui
14:09:01.695 [2155] [XNIO-3 task-1] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping [getHandler] AbstractHandlerMapping.java:420) - Mapped to public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()
14:09:01.804 [2155] [XNIO-3 task-2] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /swagger-resources/configuration/security
14:09:01.805 [2155] [XNIO-3 task-2] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping [getHandler] AbstractHandlerMapping.java:420) - Mapped to public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()
14:09:01.816 [2155] [XNIO-3 task-3] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /swagger-resources
14:09:01.818 [2155] [XNIO-3 task-3] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping [getHandler] AbstractHandlerMapping.java:420) - Mapped to public org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()
14:09:01.876 [2155] [XNIO-3 task-4] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /v2/api-docs
14:09:01.876 [2155] [XNIO-3 task-4] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [getHandler] AbstractHandlerMapping.java:420) - Mapped to public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)
14:09:01.881 [2155] [XNIO-3 task-5] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /
14:09:01.895 [2155] [XNIO-3 task-5] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /error
14:09:01.904 [2155] [XNIO-3 task-5] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping [getHandler] AbstractHandlerMapping.java:420) - Mapped to public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
14:09:01.944 [2155] [XNIO-3 task-6] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /csrf
14:09:01.955 [2155] [XNIO-3 task-6] DEBUG s.d.s.w.PropertySourcedRequestMappingHandlerMapping [lookupHandlerMethod] PropertySourcedRequestMappingHandlerMapping.java:108) - looking up handler for path: /error
14:09:01.956 [2155] [XNIO-3 task-6] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping [getHandler] AbstractHandlerMapping.java:420) - Mapped to public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
0reactions
stale[bot]commented, Jul 8, 2020

This issue has been automatically closed because it has not had recent activity. Please re-open a new issue if this is still an issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does springfox-swagger2 UI tell me "Unable to infer base ...
I had this annotation in the SwaggerConfig class but the "Unable to infer base URL" error persisted. When I moved this annotation to...
Read more >
Swagger ui stuck on unable to infer base url · Issue #1996
When I try to set the base url through this message, no matter what I enter, the dialog flickers and just stands there....
Read more >
[SpringFox Error] Unable to infer base url - Taogen's Blog
Solution. make sure your swagger configuration is under your spring boot application which can be scanned. and then it solved.
Read more >
Swagger UI Configuration with OKTA - Okta Support
Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The...
Read more >
Unable to infer base url in Swagger 2.9.2-eclipse
Coding example for the question Swagger UI - Unable to infer base url in Swagger ... app with embedded tomcat then you need...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found