springdoc openapi ui+;昂首阔步的唐';无法理解@PathVariable required=false标志
我使用此库生成文档:springdoc openapi ui+;昂首阔步的唐';无法理解@PathVariable required=false标志,spring,swagger,spring-restcontroller,springdoc,springdoc-openui,Spring,Swagger,Spring Restcontroller,Springdoc,Springdoc Openui,我使用此库生成文档: <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-ui</artifactId> <version>1.5.0</version> </dependency> 但我得到了以下文件: <dependency> <groupId&g
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.0</version>
</dependency>
但我得到了以下文件:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.0</version>
</dependency>
为什么required=false
不起作用
我试过这个:
@RestController
public class TestController {
@GetMapping("/test{hz}")
public String test(
@Parameter(description = "foo", required = false)
@PathVariable(value = "hz", required = false) String hz) {
return "test";
}
}
@PathVariable(value = "hz", required = false) Optional<String> hz
它也不起作用
编辑:(回答@Helen comment)-我当然知道这一点:
@RestController
public class TestController {
@GetMapping(value = {"/test", "/test{hz}"})
public String test(
@Parameter(description = "foo", required = false)
@PathVariable(value = "hz", required = false) String hz) {
return "test";
}
}
我试过这个:
@RestController
public class TestController {
@GetMapping("/test{hz}")
public String test(
@Parameter(description = "foo", required = false)
@PathVariable(value = "hz", required = false) String hz) {
return "test";
}
}
@PathVariable(value = "hz", required = false) Optional<String> hz
@PathVariable(value=“hz”,required=false)可选hz
它使文档更糟糕。所以我没有添加这个代码。在{”/test“,“/test{hz}}
中,它看起来是这样的:
这符合OpenAPI规范 当客户端进行API调用时,必须用实际值替换每个path参数。在OpenAPI中,使用In:path定义路径参数。参数名称必须与路径中指定的名称相同。还记得添加必需:true,因为路径参数总是必需的 您可以查看以下文档:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.0</version>
</dependency>
@PathVariable
必须有required=true
,路径参数不能标记为可选。您需要定义两个单独的方法-一个带hz
参数,另一个不带参数。@Helen看起来很奇怪。你为什么这么直截了当?Spring annotationpublic@interface PathVariable
有一个参数boolean required()默认为true我可以设置required==false
,它在Spring中工作。Swagger注释public@interface参数
具有boolean required()默认值false代码>为什么我必须使用两种不同的方法?Spring允许我使用一个方法@GetMapping(value={/test“,“/test{hz}”)
。你认为图书馆(狂妄自大)必须规定规则吗?说明路径参数必须具有必需:true
。它们不能是可选的。也许@PathVariable with required=false是其他API描述格式的一个选项,但它与OpenAPI不兼容。您需要两个单独的方法,因为/test{hz}
的方法必须具有必需的路径参数,而/test
的方法必须没有参数。