Search code examples
springrestspring-mvcswaggerspringfox

How to set host url for springfox (more exact springfox-swagger2) in spring-mvc?


I have spring-mvc app and I've embed RestAPI in. All works correctly my rest api is mapped on /rest/* url. When I added SwaggerConfig it had started to recognise my controllers, but when I tried it out in swagger-ui (gui form to simplify consumers interaction with api) enter image description here

I've got 404 not found status. Because this tried it out on enter image description here this doesnt do request on valid url

http://localhost:8080/ProductCatalog/rest/branch?id=1

although SwaggerConfig is mapped on correct url, because I've got this GUI representation when write

http://localhost:8080/ProductCatalog/rest/swagger-ui.html

There is a main part of app on root url (this isn't part in which i work) my part is mapped on /rest/* How can I change this "try it out" url on /rest/* too? My SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket pscApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                //.groupName("PSC");
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("restService.com.websystique.springmvc"))
                .paths(PathSelectors.any())
                .build();
        //PathSelectors.regex("/api/.*")
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("RestApiOfPSC")
                .description("REST API for PSC.")
                .build();
    }
}

and I've specified this too

@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/");
    }

Sorry for my bad english and thanks in advance


Solution

  • I've got how to do this.

    docket.pathMapping("/rest");
    

    and sometimes you need to change it another way in your Docket bean write docket.host("your host url"); more exactly read my issue https://github.com/springfox/springfox/issues/1468

    and go through the reference #issue1050 too.