我有下面的代码,那是我的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 的参数“required”默认为 true(因此,默认值为 @PathVariable(name="userId", required = true)),并且在该参数上没有 @ApiParam 时也能正常工作。就我而言,这些注释不应该改变彼此的行为。这是一个 Swagger 错误还是只是误用?
最佳答案
默认情况下,@ApiParam 的参数“required”为 false,因此您只需将其更改为 true 即可通过 Swagger UI 要求它。
@ApiParam(value = "The user id", required = true) @PathVariable("userId") Integer userId
关于java - Swagger @ApiParam 注释使在 Swagger UI 中不需要使用 @PathVariable 注释的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53066925/