使用Spring的@RequestMapping与多个端点

问题描述 投票:0回答:1
@RequestMapping(value = { "/abc/xyz/redirect",
            "/abc/xyz/redirect/{refPath}",
            "/abc/xyz/redirect/*" })
public String handleRequest (ServletRequest request, ServletResponse response,
@PathVariable String refPath,
@RequestParam(value = "subscriptionId", required = false) String customId)

我实现了这个逻辑来添加

refPath
,但之后我开始收到 500 且“refPath”不可用,即使端点命中是
/abc/xyz/redirect?customId=123

有人可以帮我理解为什么它没有被映射到

/abc/xyz/redirect
吗?

我在将

@PathVariable
设置为 false 后尝试工作。可以了,但还是不明白为什么与当前配置不匹配?

java spring-boot wildcard request-mapping path-variables
1个回答
0
投票

Spring MVC 中的 @PathVariable 注解表示方法参数应该绑定到 URI 模板变量。当您将多个 URI 模式映射到同一方法时,Spring 需要根据传入请求解析要绑定的变量。

在您的情况下,您将三种 URI 模式映射到handleRequest 方法:

"/abc/xyz/redirect" "/abc/xyz/redirect/{refPath}" "/abc/xyz/redirect/*" 当请求进来时,Spring需要确定匹配哪种模式。如果请求 URL 为“/abc/xyz/redirect?customId=123”,则它与没有 {refPath} 变量的第一个模式匹配,因此 refPath 保持为 null,因为它不是 URI 的一部分。

所以要么

  1. 更改映射顺序
  2. 使 refPath 可选
© www.soinside.com 2019 - 2024. All rights reserved.