`PathVariable` 在 Swagger SpringFox(3.0.0)中的可选项:

huangapple go评论58阅读模式
英文:

PathVariable as optional in swagger SpringFox(3.0.0)

问题

我最近已经升级到了最新版本的 SpringFox(3.0.0)PathVariable 标记为 required = false,但在显示中仍然被视为必需。

以下是我的控制器方法代码:

@GetMapping(path = { "/persons", "/persons/{id}" })
public List<Person> getPerson(@PathVariable(required = false) String id) {
	
}

我尝试过添加 @ApiParam,默认情况下它是 false。但是在 Swagger 上仍然显示为必需。

之前使用 SpringFox(2.9.0) 时,它正常工作,在 Swagger 上被标记为可选。

对此的任何帮助将不胜感激。
谢谢

英文:

I have upgraded to the latest version of SpringFox(3.0.0) recently.<br/>
PathVariable marked as required = false is showing as mandatory.

Below is my controller method code

@GetMapping(path = { &quot;/persons&quot;, &quot;/persons/{id} })
public List&lt;Person&gt; getPerson(@PathVariable(required = false) String id) {
	
}

I have tried adding @ApiParam, by default it is false. But still, on swagger it is showing as mandatory.

Previously with SpringFox(2.9.0) it was working fine, on swagger it was marked as optional

Any help in this will be appreciated.<br/>
Thank you

答案1

得分: 4

Path参数始终是必需的。如果我们有一个可选的路径变量,那么我们需要定义两个单独的端点。

我已经添加了两个端点来解决我的问题,如下所示。

@GetMapping(path = { "/persons" })
public List<Person> getAllPerson() {
    
}

@GetMapping(path = { "/persons/{id}" })
public List<Person> getPersonById(@PathVariable String id) {
    
}
英文:

Path parameters are always required. If we are having an optional path variable then we need to define the two separate end poinds.

I have added two endpoints to solve my problem as shown below.

@GetMapping(path = { &quot;/persons })
public List&lt;Person&gt; getAllPerson() {
    
}

@GetMapping(path = {&quot;/persons/{id} })
public List&lt;Person&gt; getPersonById(@PathVariable String id) {
    
}

huangapple
  • 本文由 发表于 2020年7月27日 12:49:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63108865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定