问题
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