【问题标题】:Using @RequestParam annotated method with swagger ui使用带有 swagger ui 的 @RequestParam 注释方法
【发布时间】:2016-01-27 10:04:47
【问题描述】:

我正在使用Springfox 库为 REST 服务生成文档并将其显示在 Swagger UI 中。我按照Springfox documentation 中的指示进行操作。

我有一个控制器,它使用查询字符串中的参数,方法映射如下:

@ApiOperation(value = "")
@RequestMapping(method = GET, value = "/customcollection/{id}/data")
public Iterable<CustomeType> getData(@ApiParam(value = "The identifier of the time series.") 
    @PathVariable String id,
    @ApiParam(name = "startDate", value = "start date", defaultValue = "")
    @RequestParam("startDate") String startDate,
    @ApiParam(name = "endDate", value = "end date", defaultValue = "")
    @RequestParam("endDate") String endDate)

然后在 swagger-ui 中生成的映射器显示为:

GET /customcollection/{id}/data{?startDate,endDate}

参数在 UI 中正确显示:

但是当我点击 Try it Out 时,请求的 URL 是错误的:

http://localhost:8080/customcollection/1/data{?startDate,endDate}?startDate=1&endDate=2

如何解决?

【问题讨论】:

    标签: spring-boot swagger-ui swagger-2.0 springfox


    【解决方案1】:

    这是由线路引起的

     enableUrlTemplating(true)
    

    Docket 配置中,我从示例中复制并忘记删除。

    删除此行后,一切都按预期工作。

    【讨论】:

    • 感谢您的回答! enableUrlTemplating 到底是做什么的?
    • 决定是否对路径使用 url 模板。当您拥有可能对每个搜索用例有多个请求映射的搜索 api 时,这尤其有用。这是一个孵化功能,在修改 swagger 规范以适应问题 #711 中所述的用例后,可能不会继续支持该功能
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    相关资源
    最近更新 更多