apigee-127 / swagger-tools

A Node.js and browser module that provides tooling around Swagger.
MIT License
701 stars 373 forks source link

how to do friendly base url for swagger 2.8.0 #567

Closed julyaTyrer closed 6 years ago

julyaTyrer commented 6 years ago

Hi,

I'm trying to change base url for API documentation. The base url is "http://localhost:8080/swagger-ui.html". I want to get something like "http://localhost:8080/myapi/swagger-ui.html".

I use Springfox 2.8.0 Swagger, Java 8, Spring Boot 2.0 The swagger configuration is:

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
    public Docket api(ServletContext servletContext) {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathProvider(new RelativePathProvider(servletContext) {
                    @Override
                    public String getApplicationBasePath() {
                        return "/myapi";
                    }
                })
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(Predicates.not(PathSelectors.regex("/error")))
                .build()
                .useDefaultResponseMessages(false);
    }
}

Custom path provider had to help, but I still get access to api documentation by using url "http://localhost:8080/swagger-ui.html". If I use url "http://localhost:8080/myapi/swagger-ui.html", I get 404 error. screenshot_1 Please, help to change the code.

alexmoleiro commented 6 years ago

See below what worked for us:

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build().pathProvider(new RelativePathProvider(null) {
                @Override
                public String getApplicationBasePath() {
                    return "/";
                }
            });
    }
}
whitlockjc commented 6 years ago

This has nothing to do with swagger-tools I'm afraid.

VahanYeghyan commented 5 years ago

alexmoleiro you saved my week)))