我在使用 Swagger 和 Spring Boot 时遇到了问题。我有一个这样的方法:
@ApiOperation(value = "Returns product of specified id")
@GetMapping("/{id}")
private ProductInShop getProduct(@ApiParam(name = "Product id", value = "Id of the product you want to get") @PathVariable String id) {
return productService.findById(id);
}
此操作在 Postman 中非常有效,当我在路径上调用 GET 方法并指定 id 时,我会获得所需的产品。但它不适用于 Swagger 。我尝试调试,发现在此操作中调用 id 获取的是 '{id}'
值,而不是产品 id。如何解决这个问题并使 PathVariable 能够正确获取?
最佳答案
问题可能与 @ApiParam
name
属性有关:要么删除它:
@ApiOperation(value = "Returns product of specified id")
@GetMapping("/{id}")
private ProductInShop getProduct(@ApiParam(value = "Id of the product you want to get") @PathVariable String id) {
return productService.findById(id);
}
或者提供适当的值:
@ApiOperation(value = "Returns product of specified id")
@GetMapping("/{id}")
private ProductInShop getProduct(@ApiParam(name = "id", value = "Id of the product you want to get") @PathVariable String id) {
return productService.findById(id);
}
关于java - Swagger未获取PathVariable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67828906/