【发布时间】: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