Swagger @ApiParam 注释使参数用 @PathVariable 注释在 Swagger UI 中不需要

lop*_*hen 1 java spring swagger

我有以下代码,这是我的 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";
        }
Run Code Online (Sandbox Code Playgroud)

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

Jan*_*nar 7

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

@ApiParam(value = "The user id", required = true) @PathVariable("userId") Integer userId
Run Code Online (Sandbox Code Playgroud)