在路径中招摇多个版本 [英] swagger multiple versions in path
问题描述
我在控制器中有以下 spring get 映射(org.springframework.web.bind.annotation.GetMapping
):
I have the following spring get mapping (org.springframework.web.bind.annotation.GetMapping
) in a controller:
@GetMapping("/v{version:[1-2]}/something/{id}")
我希望能够在 swagger 中访问两个版本的 api.这是我的招摇配置:
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()
}
这不起作用,当我删除 paths
选择器时,我唯一能看到的 swagger 是:
This does not work, the only thing I can see in swagger when I remove the paths
selector is:
/v{version}/something/{id}
我想看看:
/v1/something/{id}
在swagger组选择器中选择V1
组时:这在选择 V2
时:
When selecting the V1
group in the swagger group selector:
And this when selecting V2
:
/v2/something/{id}
推荐答案
实际上,您可能需要实现自定义PathProvider 解开映射路径 "/v{version:[1-2]}/something/{id}"
像这样在 Docket 中变成特定的:
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);
}
}
这篇关于在路径中招摇多个版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!