Springfox 没有为相同的操作端点、不同的查询参数正确生成 Swagger 文档
Springfox not generating Swagger docs correctly for same operation-endpoint, different query parameters
Springfox 无法为像这样的简单案例正确生成 swagger 文档:
GET /api/departments - Gets all department
GET /api/departments?name=IT - Gets a department with name passed as query parameter
这是 Spring 控制器:
@ApiOperation(value = "Gets all departments", notes = "", tags = {"departments"})
@RequestMapping(value="/departments", produces=MediaType.APPLICATION_JSON_VALUE, method=RequestMethod.GET)
public List<Department> getAllDepartments(){
...
}
@ApiOperation(value = "Gets a department by name", notes = "", tags = {"departments"})
@RequestMapping(value="/departments", params="name", produces=MediaType.APPLICATION_JSON_VALUE, method=RequestMethod.GET)
public Department getDepartmentByName(@RequestParam String name){
...
}
生成的 swagger 文件仅包含 GET /api/departments 条目,没有包含查询过滤器的痕迹。
任何解决方法?
Swagger 配置:
@Bean
public Docket departmentsV1Api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("/.*"))
.build()
.apiInfo(new ApiInfo("Departments Rest API","","v1","","","",""))
.pathMapping("/api")
.securitySchemes(newArrayList(apiKey()))
.securityContexts(newArrayList(securityContext()))
.groupName("departmentsV1");
}
规范不支持此用例。但是,您可以在 docket
中使用 enableUrlTemplating(true)
方法来启用 rfc6570 支持。
还有一个实验库 (springfox-swagger-ui-rfc6570),可用作标准 springfox-swagger-ui
的直接替代品。
注意: 请记住,这还在孵化阶段,在规范中添加支持后可能会发生变化。
Springfox 无法为像这样的简单案例正确生成 swagger 文档:
GET /api/departments - Gets all department
GET /api/departments?name=IT - Gets a department with name passed as query parameter
这是 Spring 控制器:
@ApiOperation(value = "Gets all departments", notes = "", tags = {"departments"})
@RequestMapping(value="/departments", produces=MediaType.APPLICATION_JSON_VALUE, method=RequestMethod.GET)
public List<Department> getAllDepartments(){
...
}
@ApiOperation(value = "Gets a department by name", notes = "", tags = {"departments"})
@RequestMapping(value="/departments", params="name", produces=MediaType.APPLICATION_JSON_VALUE, method=RequestMethod.GET)
public Department getDepartmentByName(@RequestParam String name){
...
}
生成的 swagger 文件仅包含 GET /api/departments 条目,没有包含查询过滤器的痕迹。
任何解决方法?
Swagger 配置:
@Bean
public Docket departmentsV1Api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("/.*"))
.build()
.apiInfo(new ApiInfo("Departments Rest API","","v1","","","",""))
.pathMapping("/api")
.securitySchemes(newArrayList(apiKey()))
.securityContexts(newArrayList(securityContext()))
.groupName("departmentsV1");
}
规范不支持此用例。但是,您可以在 docket
中使用 enableUrlTemplating(true)
方法来启用 rfc6570 支持。
还有一个实验库 (springfox-swagger-ui-rfc6570),可用作标准 springfox-swagger-ui
的直接替代品。
注意: 请记住,这还在孵化阶段,在规范中添加支持后可能会发生变化。