user09
user09

Reputation: 956

Adding base path to swagger documentaion

I am trying to change the base path of swagger codumentation. Currently I have

@RequestMapping(path = "/api/resourceName/v1")

and swagger config

 return new Docket(DocumentationType.SWAGGER_2).
                select()
                .apis(RequestHandlerSelectors.basePackage("com.company"))
                .paths(PathSelectors.ant("/api/**"))
                .build()
                .apiInfo(apiInfo());

This is giving swagger base path as "basePath": "/"

I want to add base path as "basePath": "/api" so I followed diff threads like this How to change basePath for Springfox Swagger 2.0 and added

return new Docket(DocumentationType.SWAGGER_2).
                select()
                .apis(RequestHandlerSelectors.basePackage("com.company"))
                .paths(PathSelectors.ant("/api/**"))
                .build()
                .apiInfo(apiInfo())
                .pathProvider(new RelativePathProvider(servletContext) {
                    @Override
                    public String getApplicationBasePath() {
                        return "/api";
                 }
            });

now the base path is changed to "basePath": "/api" and I updated my path mapping to @RequestMapping(path = "/resourceName/v1") as base has been added.

When I send the request from swagger, the request is going to /api/resourceName/v1 but the service returns 404.

When I send the request through postman for /resourceName.v1 then it works.

So the api is registred as /resourceName/v1 and the base is just added by swagger on top of it and will not work if the request sent throguh swagger UI

Then I added server.servlet-path=/api to application.properties to register basepath in the request mapping and now swagger shows the base path as /api without needing additional configuration.

But the problem is now swagger documentation is available at http://localhost:8080/api/swagger-ui.html instead of http://localhost:8080/swagger-ui.html. As we have all our other services doc at http://host/swagger-ui.html this is not useful.

Is there any way to add the base and still access the doc at http://host/swagger-ui.html and api's works as expected fro swagger and postman

Upvotes: 2

Views: 9092

Answers (2)

Atul
Atul

Reputation: 3347

There is way to change the api-doc base path using properties defined in application.properties file.

The properties are:

springfox.documentation.openApi.v3.path=/external/api/app/v3/api-docs
springfox.documentation.swaggerv2.path=/external/api/app/v2/api-docs

This can help you to change the path.

Upvotes: 0

kadir
kadir

Reputation: 599

Yes, you can add base path for all swagger requests. I used following config for this purpose:

@Configuration
@EnableSwagger2
public class SpringFoxConfig {
    @Bean
    public Docket api() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();

        docket.pathMapping("api");

        return docket;
    }
}

Hope this helps.

Upvotes: 2

Related Questions