Springfox没有为相同的操作端点,不同的查询参数正确生成Swagger文档

时间:2015-11-16 11:52:31

标签: spring spring-mvc swagger swagger-2.0 springfox

对于像这样的简单案例,Springfox无法正确生成swagger文档:

GET /api/departments - Gets all department

GET /api/departments?name=IT - Gets a department with name passed as query parameter

这是Spring Controller:

@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");
}

2 个答案:

答案 0 :(得分:2)

规范不支持此用例。但是,您可以使用enableUrlTemplating(true)中的docket方法启用rfc6570支持。

还有一个实验库(springfox-swagger-ui-rfc6570),可用作标准springfox-swagger-ui的替代品。

注意: 请记住,这是在孵化中,并且可以在规范中添加支持时更改。

答案 1 :(得分:0)

您需要使用@ApiParam注释方法参数,例如:

@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(@ApiParam(name = "name", value = "name") @RequestParam String name){
    ...
}