0

When I was using springfox-swagger 2.9.0, I was using below code in my project.

 @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        @Bean
        public Docket api() {
        Docket docket = null;
    try{
     if(!(profile.contains("local")|| (profile.contains("test"))
            docket = new Docket(DocumentationType.SWAGGER_2)
    .host(host)
    .pathProvider(new RelativePathProvider(servletContext){
    @Override
    public String getApplicationBasePath(){
    
    return "/api";
    }
    })
    .select()
    .apis(RequestHandlerSelectors.basePackage("org.app.controller"))
    .paths(PathSelectors.any())
    .build();
        }
    else{
    docket = new Docket(DocumentationType.SWAGGER_2)
    .host(host)
    .select()
    .apis(RequestHandlerSelectors.basePackage("org.app.controller"))
    .paths(PathSelectors.any())
    .build();
    }
    }
    catch(Exception e){
    logger.info("Unable to return docket",ex)
    }
    return docket;
    }
    }

After adding below swagger 3.0.0 dependency, my updated class is:

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



@Configuration
public class SwaggerConfig {

    @Bean
    public Docket api() {
    Docket docket = null;
try{
 if(!(profile.contains("local")|| (profile.contains("test"))
        docket = new Docket(DocumentationType.SWAGGER_2)
.host(host)
.pathProvider(new PathProvider(){
@Override
public String getOperationPath(String operationPath){
return operationPath.replace("/api","");
}

@Override
public String getResourceListingPath(String groupName, String apiDeclaration){
return null;
}
})
.select()
.apis(RequestHandlerSelectors.basePackage("org.app.controller"))
.paths(PathSelectors.any())
.build();
    }
else{
docket = new Docket(DocumentationType.SWAGGER_2)
.host(host)
.select()
.apis(RequestHandlerSelectors.basePackage("org.app.controller"))
.paths(PathSelectors.any())
.build();
}
}
catch(Exception e){
logger.info("Unable to return docket",ex)
}
return docket;
}
}

After using this code , I am not able to append "/api" to my baseurl "localhost:8080" from updated swagger url. http://localhost:8080/abc-api/swagger-ui/index.html#/

The base url should appear as "localhost:8080/api".

I tried creating separate bean for PathProvider implementation and then passed in argument but still same issue is there.

Could anyone please tell me what am I doing wrong here and how to create the baseurl as "/api" instead or "/" ?

Sachin Pandey
  • 47
  • 1
  • 8

1 Answers1

0

This is not an answer to your problem but it might help you. Since you are migrating anyway, consider using springdoc instead of Springfox. It is a newer library that is easier to use and way less error-prone than Springfox. We moved to it 2 years ago and we are very glad we did. There is very good documentation and tutorials online for it:

It is also very active and you usually get your issues answered very fast on the github page.

João Dias
  • 16,277
  • 6
  • 33
  • 45
  • It is mandatory to use springfox only in my project as per company standards. I cannot simply change the library. So looking for any solution related to springfox-swagger library – Sachin Pandey Oct 26 '21 at 08:37
  • Fair enough ;) In the future consider `springdoc` if you are able to change it. – João Dias Oct 26 '21 at 08:38