【问题标题】:Swagger @ApiParam annotation makes parameter annotated with @PathVariable non-required in Swagger UISwagger @ApiParam 注释使在 Swagger UI 中使用 @PathVariable 注释的参数不再是必需的
【发布时间】:2018-10-30 14:46:32
【问题描述】:

我有以下代码,那是我的API的艺术

    import org.springframework.web.bind.annotation.PathVariable;
    import io.swagger.annotations.*;

    @GetMapping(value = "/{userId}")
        public String getUserHistory(ApiParam(value = "The user id") 
                    @PathVariable("userId") Integer userId, Model model) throws NotFoundException {
            // fetch user info
            model.addAttribute("userInfo", userInfo);
            return "userHistory";
        }

如果我有@ApiParam 注释,@PathVariable 将变为非必需,因此如果我不输入 userId 并通过 Swagger UI 发出请求,请求仍会发送到服务器,这会导致不需要的服务器错误。 @PathVariable 的“必需”参数默认为 true(因此,默认值为 @PathVariable(name="userId", required = true)),并且在该参数上没有 @ApiParam 时可以正常工作。就我而言,这些注释不应该改变彼此的行为。这是 Swagger 的错误还是只是误用?

【问题讨论】:

  • Swagger 版本?
  • from this 听起来很愚蠢,不是吗? > 还要记得加上 required: true,因为路径参数总是需要的。
  • @lealceldeiro 2.7.0

标签: java spring swagger


【解决方案1】:

@ApiParam 的“必需”参数默认为 false,因此您只需将其更改为 true 即可通过 Swagger UI 要求它。

@ApiParam(value = "The user id", required = true) @PathVariable("userId") Integer userId

【讨论】:

    猜你喜欢
    • 2014-12-26
    • 2020-04-04
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 2016-01-27
    • 1970-01-01
    • 2016-12-30
    相关资源
    最近更新 更多