java - Swagger未获取PathVariable

标签 java spring spring-boot spring-mvc swagger

我在使用 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/

相关文章:

java - 我怎样才能用代码进行java对象分配跟踪

java - 将数字转换为具有完整填充的二进制字符串?

java - Spring 安全: How to get the automatically rendered login page code?

spring-boot - Spring Boot 和 Kafka : Failed to send SSL Close message

java - Azure 根据 Azure B2C Activity 目录验证 JWT token

java - 将 war 文件从 Jenkins 部署到弹性 beanstalk 时部署失败

java - Gradle 禁用所有增量编译和并行构建

java - 认证前检查Spring-Security中的X509证书吊销状态

spring - Maven/ Spring : Automatic Test Run of Generated WAR

hibernate - JpaSort.unsafe 抛出异常