0

I use Springboot with swagger 3:

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

I use a default /api prefix to all my endpoints.

This is how I configured my SwaggerConfig:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    public static final String AUTHORIZATION_HEADER = "Authorization";
    @Bean
    public Docket api() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .securityContexts(Collections.singletonList(securityContext()))
                .securitySchemes(Collections.singletonList(apiKey()))
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build().pathMapping("/api");

        return docket;
    }

    private ApiKey apiKey() {
        return new ApiKey("JWT", AUTHORIZATION_HEADER, "header");
    }
  // ......

}

When I try to access to my swagger-ui http://myhost/swagger-ui I get a popup with this message 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: asking me to define the location with.

When I enter my prefix manually : http://myhost/api then every thing is fine.

Any idea how to define my REST API prefix ?

Helen
  • 71,797
  • 10
  • 199
  • 256
medkhelifi
  • 991
  • 2
  • 13
  • 31
  • Hi, I had the same issue and the solution described on https://stackoverflow.com/a/53437951/924036 seems to have solved this for me. – Kaj Hejer Jan 02 '21 at 15:13

1 Answers1

0

I also come across your problem, and I use swagger 3 too. finally, I found it is the @RestControllerAdvice cause the problem, I use the @RestControllerAdvice to control a unified response body.

@RestControllerAdvice
public class ResponseAdvisor implements ResponseBodyAdvice {
    
      @Override
      public boolean supports(MethodParameter methodParameter, Class aClass) {
        return true;
      }
    
      @Override
      public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
       // do something to package response body o
    }

so I modify it to @RestControllerAdvice(basepackage="package of project") to limit the scope. It does work finally. So check you have use @RestControllerAdvice or not, if use, add a basepackage to limit the scope, or it will infuluence swagger 3. Hope this can help you. finally,sorry for my poor English.

pfas
  • 66
  • 2