我在使用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时,我会得到所需的产品。但招摇是行不通的。我试着调试,发现在这个操作中,调用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);
}