Can we have two @PathVariable. One as the actual Path Variable and other for swagger to document it as Deprecated?

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

Can we have two @PathVariable. One as the actual Path Variable and other for swagger to document it as Deprecated?

问题

我正在尝试更改REST调用的@PathVariable。现有的路径变量是由三个参数组合而成的。更改的目的是处理由两个参数组合而成的路径变量。我需要在Swagger文档中记录此更改,同时将旧的标记为已弃用。
我尝试过使用两个@PathVariable,其中一个标记为@Deprecated,如下所示:

@Parameter(description = "x_y_z - x is first ID, y is second ID, z is third ID", required=false )
@Deprecated @PathVariable String x_y_z,
@Parameter(description = "x_y - x is first ID, y is second ID", required=true )
@PathVariable String x_y)

我已将请求映射值从/aaa/bbb/{x_y_z}更改为以下内容:

@RequestMapping(value = "/aaa/bbb/{x_y}", method = RequestMethod.GET, produces = "application/json")

经过上述更改,请求失败并返回500错误,可能是因为它预期接收两个路径变量。但Swagger文档显示正常。

我尝试删除了x_y_z的@PathVariable注释。请求得到了预期的处理,并且Swagger现在将x_y_z标记为已弃用,但参数显示为*(query)而不是(path)*。

有什么建议吗?

英文:

I am trying to change the REST call's @PathVariable. The existing Path Variable is formed by combination of three parameters. The change is to handle Path Variable formed by combination of two parameters. I need this change to be documented in swagger with the earlier shown as Deprecated.
I have tried to use both Path Variable with one as @Deprecated like below

@Parameter(description = "x_y_z - x is first ID, y is second ID, z is third ID", required=false )
            @Deprecated @PathVariable String x_y_z,
@Parameter(description = "x_y - x is first ID, y is second ID", required=true )
            @PathVariable String x_y)

The have changed request mapping value from /aaa/bbb/{x_y_z} to below

@RequestMapping(value = "/aaa/bbb/{x_y}", method = RequestMethod.GET, produces = "application/json"

With above changes the request fails with 500 error, may be since it is expecting two Path Variables. But the swagger documentation is as expected.

I tried to remove @PathVariable for x_y_z. The request is processed as expected and the swagger now shows x_y_z as deprecated but shows the parameter as (query) instead of (path)

Any suggestions please

答案1

得分: 1

假设有一个@RestController,并且Swagger可以理解方法上的@Deprecated注解:

@Deprecated
@GetMapping("/aaa/bbb/{x:\\d+}_{y:\\d+}_{z:\\d+}")
public ResponseEntity<MessageResponse> getStuff(@PathVariable String x,
                                                @PathVariable String y,
                                                @PathVariable(name = "z", required = false) String z) {

    return getNewStuff(x, y); // 发送到另一个方法并忽略 z
}

@GetMapping("/aaa/bbb/{x:\\d+}_{y:\\d+}")
public ResponseEntity<MessageResponse> getNewStuff(@PathVariable String x,
                                                   @PathVariable String y) {
    // 默认情况下处理 x 和 y
    return ResponseEntity.ok(new MessageResponse("此方法支持 " + x + " 和 " + y));
}

正则表达式应该查找作为路径变量的数字,并用下划线分隔。

注意:如果Swagger支持它而不是理解它,可以考虑它可能已被弃用的情况:

@PathVariable @Parameter(description = "x_y_z - x is first ID, y is second ID, z is third ID", deprecated = true) String z

弃用原始方法并引入具有正确参数但不同RequestMapping的新方法也可以是有效的解决方法。

另一个要注意的部分是,在Spring中使用斜杠作为分隔符比下划线更常见(例如,/aaa/bbb/x/y)。您还可以根据您的要求包含适合您需求的验证器。

英文:

Assuming an @RestController and that Swagger understands @Deprecated for a method:

@Deprecated
@GetMapping(&quot;/aaa/bbb/{x:\\d+}_{y:\\d+}_{z:\\d+}&quot;)
public ResponseEntity&lt;MessageResponse&gt; getStuff(@PathVariable String x,
                                                @PathVariable String y,
                                                @PathVariable(name = &quot;z&quot;, required = false) String z) {

    return getNewStuff(x, y); //send to the other method and ignore z
}

@GetMapping(&quot;/aaa/bbb/{x:\\d+}_{y:\\d+}&quot;)
public ResponseEntity&lt;MessageResponse&gt; getNewStuff(@PathVariable String x,
                                                   @PathVariable String y) {
    // do stuff for x and y by default
    return ResponseEntity.ok(new MessageResponse(&quot;this method is supported for &quot; + x + &quot; and &quot; + y));
}

The RegEx should look for digits as the path variables, interspersed with underscores.

NB: leaving this part of the answer if Swagger works with it instead with the understanding that it could be deprecated:

@PathVariable @Parameter(description = &quot;x_y_z - x is first ID, y is second ID, z is third ID&quot;, deprecated = true) String z

Deprecating the original method and introducing a new method with the correct parameters but different RequestMapping could also be a valid workaround.

The other part to note is that it is more common to use slashes as the delimiter rather than underscores in Spring (e.g., /aaa/bbb/x/y). You also may wish to include a validator that fits your requirements.

huangapple
  • 本文由 发表于 2023年1月9日 19:01:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056327.html
匿名

发表评论

匿名网友

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

确定