swagger multiple versions in path

被刻印的时光 ゝ 提交于 2019-12-25 03:07:22

问题


I have the following spring get mapping (org.springframework.web.bind.annotation.GetMapping) in a controller:

@GetMapping("/v{version:[1-2]}/something/{id}")

I want to be able to access the two versions of the api in swagger. This is my swagger config:

@Bean
public Docket v1(SwaggerProperties swaggerProperties) {
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("V1")
        .select()
            .paths(regex("/v1/.*")).build()     
}

@Bean
public Docket v2(SwaggerProperties swaggerProperties) {
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("V2")
        .select()
            .paths(regex("/v2/.*")).build()   
}

This does not work, the only thing I can see in swagger when I remove the paths selector is:

/v{version}/something/{id}

And I would like to see:

/v1/something/{id}

When selecting the V1 group in the swagger group selector: And this when selecting V2:

/v2/something/{id}

回答1:


Actually, you might need to implement custom PathProvider to unwrap the mapping path "/v{version:[1-2]}/something/{id}" into particular one in Docket like that:

//in Docket
.pathProvider(new ParticularVersionPathProvider("v1"))

...
class ParticularVersionPathProvider extends AbstractPathProvider {
   ...
   private String version;
   BasePathAwareRelativePathProvider(String version){
       this.version = version;
   }

   @Override
   public String getOperationPath(String operationPath) {
       //very schematically
       return operationPath.replace("v{version}",version);
   }
}

see this complete example



来源:https://stackoverflow.com/questions/51982295/swagger-multiple-versions-in-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!