Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to infer base url... springfox-swagger2 version 2.9.2 #3001

Closed
1 task
lombocska opened this issue May 22, 2019 · 10 comments
Closed
1 task

Unable to infer base url... springfox-swagger2 version 2.9.2 #3001

lombocska opened this issue May 22, 2019 · 10 comments
Labels
Milestone

Comments

@lombocska
Copy link

lombocska commented May 22, 2019

  • Version of springfox-swagger that I am using Download

What kind of issue is this?
Project structure:
SwaggerConfig is in automate.api.config.swagger.SwaggerConfiguration
Auth Config is in automate.api.web.auth.keycloak.FalconKeycloakConfigurerAdapter

image

dependencies

As for Spring Boot

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

As for swagger

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>${spring.springfox-swagger2.version}</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>${spring.springfox-swagger2.version}</version>
</dependency>

Swagger 2 config

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
               .pathMapping("/");
    }
}

Security config

@Override
  protected void configureAccessRules(final HttpSecurity http) throws Exception {
    http.csrf()
        .disable()
        .authorizeRequests()
        .antMatchers("/actuator/health")
        .permitAll()
       .antMatchers(HttpMethod.GET,
                    "/v2/api-docs",
                    "/swagger-resources/**",
                    "/swagger-ui.html**",
                    "/webjars/**",
                    "favicon.ico")
            .permitAll();
  }

Example for one of my controllers
All of my controllers mapped like /api/...

@RestController
@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ExampleResource {
    private ExampleFacade catalog;

    @Autowired
    public ExampleResource(ExampleFacade catalog) {
        this.catalog = catalog;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/examples")
    public List<ExampleDTO> index() {
        return catalog.listExamples();
    }
}

ISSUE

/v2/api-docs works perfectly, I get back all of my endpoints with their details
/swagger-ui.html gives me back a pop up that shows "Unable to infer base url......"

Then I turned on the TRACE log level on my spring-boot app, like

logging:
  level:
    org:
      springframework:
        web: TRACE

My logs looks like:

[2019-05-24 14:16:41] TRACE [nio-8080-exec-1] o.s.w.s.DispatcherServlet GET "/swagger-ui.html", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-24 14:16:41] TRACE [nio-8080-exec-1] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]] and 5 interceptors
[2019-05-24 14:16:41] TRACE [nio-8080-exec-1] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-24 14:16:41] DEBUG [nio-8080-exec-1] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-24 14:16:41] TRACE [nio-8080-exec-2] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/springfox.css?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-24 14:16:41] TRACE [nio-8080-exec-3] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/swagger-ui.css?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-24 14:16:41] TRACE [nio-8080-exec-2] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-24 14:16:41] TRACE [nio-8080-exec-2] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-24 14:16:41] TRACE [nio-8080-exec-3] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-24 14:16:41] TRACE [nio-8080-exec-3] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-24 14:16:41] TRACE [nio-8080-exec-2] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-24 14:16:41] DEBUG [nio-8080-exec-2] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-24 14:16:41] TRACE [nio-8080-exec-4] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/swagger-ui-bundle.js?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-24 14:16:41] TRACE [nio-8080-exec-3] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-24 14:16:41] DEBUG [nio-8080-exec-3] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-24 14:16:41] TRACE [nio-8080-exec-4] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-24 14:16:41] TRACE [nio-8080-exec-4] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-24 14:16:41] TRACE [nio-8080-exec-5] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/swagger-ui-standalone-preset.js?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-24 14:16:41] TRACE [nio-8080-exec-6] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/springfox.js?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-24 14:16:41] TRACE [nio-8080-exec-5] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-24 14:16:41] TRACE [nio-8080-exec-5] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-24 14:16:41] TRACE [nio-8080-exec-6] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-24 14:16:41] TRACE [nio-8080-exec-6] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-24 14:16:41] TRACE [nio-8080-exec-6] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-24 14:16:41] DEBUG [nio-8080-exec-6] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-24 14:16:41] TRACE [nio-8080-exec-5] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-24 14:16:41] DEBUG [nio-8080-exec-5] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-24 14:16:41] TRACE [nio-8080-exec-4] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-24 14:16:41] DEBUG [nio-8080-exec-4] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-24 14:16:41] TRACE [nio-8080-exec-7] o.s.w.s.DispatcherServlet GET "/swagger-resources/configuration/ui", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-24 14:16:41] TRACE [nio-8080-exec-7] o.s.w.s.m.m.a.RequestMappingHandlerMapping Mapped to public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()
[2019-05-24 14:16:41] TRACE [nio-8080-exec-7] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: []
[2019-05-24 14:16:41] DEBUG [nio-8080-exec-7] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json', given [application/json] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-24 14:16:41] TRACE [nio-8080-exec-7] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data=springfox.documentation.swagger.web.UiConfiguration@735d600f)]
[2019-05-24 14:16:41] TRACE [nio-8080-exec-7] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-24 14:16:41] DEBUG [nio-8080-exec-7] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-24 14:16:41] TRACE [nio-8080-exec-8] o.s.w.s.DispatcherServlet GET "/swagger-resources/configuration/security", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-24 14:16:41] TRACE [nio-8080-exec-8] o.s.w.s.m.m.a.RequestMappingHandlerMapping Mapped to public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()
[2019-05-24 14:16:41] TRACE [nio-8080-exec-8] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: []
[2019-05-24 14:16:41] DEBUG [nio-8080-exec-8] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json', given [application/json] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-24 14:16:41] TRACE [nio-8080-exec-8] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data=springfox.documentation.swagger.web.SecurityConfiguration@250a3a2f)]
[2019-05-24 14:16:41] TRACE [nio-8080-exec-8] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-24 14:16:41] DEBUG [nio-8080-exec-8] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-24 14:16:41] TRACE [nio-8080-exec-9] o.s.w.s.DispatcherServlet GET "/swagger-resources", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-24 14:16:41] TRACE [nio-8080-exec-9] o.s.w.s.m.m.a.RequestMappingHandlerMapping Mapped to public org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()
[2019-05-24 14:16:41] TRACE [nio-8080-exec-9] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: []
[2019-05-24 14:16:41] DEBUG [nio-8080-exec-9] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json', given [application/json] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-24 14:16:41] TRACE [nio-8080-exec-9] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data=[springfox.documentation.swagger.web.SwaggerResource@54d9c9ca])]
[2019-05-24 14:16:41] TRACE [nio-8080-exec-9] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-24 14:16:41] DEBUG [nio-8080-exec-9] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}

image

Please, help me ! :)

@lombocska lombocska changed the title Unable to infer base url... springfox-swagger 2 2.9.2 Unable to infer base url... springfox-swagger-2 version 2.9.2 May 22, 2019
@lombocska lombocska changed the title Unable to infer base url... springfox-swagger-2 version 2.9.2 Unable to infer base url... springfox-swagger2 version 2.9.2 May 22, 2019
@Toerktumlare
Copy link

Toerktumlare commented May 24, 2019

there seems to be a bug that when the front end spring fox gui is trying to fetch it's configuration from the back end that if you don't explicitly set the path mapping to a value the configuration url for the front end will be built with a null value.

The error handling for this scenario seems poor, so that if the front end can\t find it, it will just go in an endless loop in the background hogging resources/threads.

i solved this by setting the path mapping:

@EnableSwagger2
public class SwaggerConfiguration {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .pathMapping("/") <----- This line of code
                .build();
    }
}

i didn't manage to exactly debug the code to where this is happening, but the bug still exists and no one has confirmed it for me. As the springfox project seems very dormant.

@lombocska
Copy link
Author

Thanks @Tandolf !! :)

Now, the path mapping does not contain anything, like /null/swagger-... :)
But the ui is not working at all ...
I hope somebody from springfox will help meg. :)

@lombocska
Copy link
Author

Now, I managed to solve my issue.
One of the issues was the pathMapping, as you saw above. :)

I needed to change my configuration to explicitly to "/", like here

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
  @Bean
  public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors.any())
        .paths(PathSelectors.any())
        .build()
        .pathMapping("/");
  }
}

Other issue was I used ResponseBodyAdvice

import io.falcon.automate.api.web.views.BaseView;
import io.falcon.automate.api.web.views.ErrorView;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;


@ControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    /**
     * Wraps all responses in base view class
     * @param body
     * @param returnType
     * @param selectedContentType
     * @param selectedConverterType
     * @param request
     * @param response
     * @return
     */
    @Override
    public Object beforeBodyWrite(
            Object body,
            MethodParameter returnType,
            MediaType selectedContentType,
            Class<? extends HttpMessageConverter<?>> selectedConverterType,
            ServerHttpRequest request,
            ServerHttpResponse response
    ) {
        if (body instanceof ErrorView) {
            return body;
        }
        return new BaseView(body);
    }
}

If I commented out my response wrapper class, everything works like a charm.....

The only issue, that I need my response wrapper. :/

@Vanquish59
Copy link

Can anyone help me on this issue... it's been days im stuck on it. Getting /null on all context paths

@willbjones
Copy link

I had this issue and the above pathmapping worked for me. Also before this I came across some more advice to remove @EnableMvc annotation from my web config and voila /swagger-ui.html works.

@jayzh1010
Copy link

Now, I managed to solve my issue.
One of the issues was the pathMapping, as you saw above. :)

I needed to change my configuration to explicitly to "/", like here

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
  @Bean
  public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors.any())
        .paths(PathSelectors.any())
        .build()
        .pathMapping("/");
  }
}

Other issue was I used ResponseBodyAdvice

import io.falcon.automate.api.web.views.BaseView;
import io.falcon.automate.api.web.views.ErrorView;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;


@ControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    /**
     * Wraps all responses in base view class
     * @param body
     * @param returnType
     * @param selectedContentType
     * @param selectedConverterType
     * @param request
     * @param response
     * @return
     */
    @Override
    public Object beforeBodyWrite(
            Object body,
            MethodParameter returnType,
            MediaType selectedContentType,
            Class<? extends HttpMessageConverter<?>> selectedConverterType,
            ServerHttpRequest request,
            ServerHttpResponse response
    ) {
        if (body instanceof ErrorView) {
            return body;
        }
        return new BaseView(body);
    }
}

If I commented out my response wrapper class, everything works like a charm.....

The only issue, that I need my response wrapper. :/

thank you very much, i have trouble with this. because of your answer, i remove my ResponseBodyAdvice, and it run successfully

@stale
Copy link

stale bot commented Jun 24, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 24, 2020
@ashokkumarprajapati
Copy link

This issue still persists.

@stale stale bot removed the wontfix label Jul 2, 2020
@dilipkrish
Copy link
Member

This issue is fixed in 3.0.0 which will be released shortly. Also please note changes coming in the next release as regards swagger-ui #3360

@dilipkrish dilipkrish added this to the 3.0 milestone Jul 3, 2020
@stale stale bot removed the wontfix label Jul 3, 2020
@dilipkrish
Copy link
Member

Closing this as a duplicate of #3360

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants