【问题标题】:Rest API with mix of Path Param and RequestParam混合了 Path Param 和 RequestParam 的 Rest API
【发布时间】:2020-03-04 14:44:25
【问题描述】:

我被要求构建一个混合了路径参数和请求参数的 Rest 端点,看起来像 -

/user/{user}?refresh={refresh}

请求参数应该是可选的。

我尝试过String getUser(@PathVariable String user, @RequestParam Map<String, String> params);,但它使 RequestParam 成为强制性的(如 Swagger UI 中所示)。

我怎样才能使它成为可选的?

【问题讨论】:

  • 顺便说一句,refresh 被称为查询参数 - Spring 所谓的“请求参数”只是多种类型参数(表单、查询等)的包装器
  • @Smutje 我没有看到@QueryParam 注释。是否有另一种方法来注释查询参数类型?

标签: java spring-boot swagger-ui


【解决方案1】:

@RequestParam注解中设置requiredfalse,如下:

@GetMapping("/user/{user}?refresh={refresh}")
String getUser(@PathVariable String user, @RequestParam(required = false) String refresh) {
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 2018-09-07
    • 2012-09-30
    • 2013-10-14
    • 2015-07-16
    相关资源
    最近更新 更多