spring boot 请求一个 url 会自动调用另一个的诡异问题

2022-08-19 12:14:29 +08:00
 FormatToday

调用第二个接口的时候为什么会同时调用第一个接口啊,不是前端写的问题,用 postman 调用也是这样。 求大佬指点一下排查的思路

2017 次点击
所在节点    Java
16 条回复
wuvvu
2022-08-19 12:20:32 +08:00
语义重复
fzdwx
2022-08-19 12:21:39 +08:00
你 debug 看看`idCode`是啥

@PathVariable 可能是匹配到了 你加个前缀比如 /getInfo/{idCode}
yfugibr
2022-08-19 12:34:08 +08:00
因为 getRecCompanyLogin 也是一个 string
ychenenen
2022-08-19 12:34:38 +08:00
{idCode}可以是 getRecCompanyLoginInfo 吧,又都是 get 请求
yfugibr
2022-08-19 12:39:08 +08:00
@yfugibr 就算 idcode 不是 string 应该也会被调用

接口设计有问题,/aurl 既能匹配 /aurl 也能匹配 /{code}
我隐约记得这么写编译是会报错的
willxiang
2022-08-19 13:32:34 +08:00
第一个接口 url 改成 idCode/{idCode}
nothingistrue
2022-08-19 13:41:23 +08:00
/{idCode} 能够匹配任何 /*** 的 URL 。
lazyfighter
2022-08-19 13:58:31 +08:00
按照优先级匹配也是第二个接口优先级较高一些, 比较好奇两个接口的执行顺序,
/**
* Look up the best-matching handler method for the current request.
* If multiple matches are found, the best match is selected.
* @param lookupPath mapping lookup path within the current servlet mapping
* @param request the current request
* @return the best-matching handler method, or {@code null} if no match
* @see #handleMatch(Object, String, HttpServletRequest)
* @see #handleNoMatch(Set, String, HttpServletRequest)
*/
@Nullable
protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {

比较好奇楼主用的什么版本的 spirng ,可以看一下这个方法是否调用了
ConfusedBiscuit
2022-08-19 14:06:55 +08:00
我很好奇如果两个方法同时被调用了,那么 HTTP 请求的返回是哪个方法的结果呢?如果是其中一个方法的返回值,那么另一个方法的返回值就被丢弃了吗?
zzl22100048
2022-08-19 14:11:00 +08:00
spring 正常不会请求一次调用两个路由的,全匹配是调用第二个,其他情况调用第一个,具体是什么版本的 spring ?
maocat
2022-08-19 14:15:58 +08:00
你没有问题,都是框架的问题.dog
siweipancc
2022-08-19 16:56:03 +08:00
就算是框架 bug ,debug 不会吗……请求源对比一下
intmax2147483647
2022-08-19 17:21:33 +08:00
有一说一,养成习惯处理一下 IDE 抛黄的警告,会减少你这类问题的提问。`idCode`, `/getRecCompanyLoginInfo`都报警告了都不管
lichao
2022-08-19 17:33:36 +08:00
idCode 可以用正则限制一下
hyqCrystal
2022-08-22 09:27:16 +08:00
建议写这种风格的时候 可以把路径搞长一点 增加接口名的特殊性
Huizhen
2022-08-23 15:05:34 +08:00
第一个 RequestMapping 的 Value 值这样写不严谨,至少加个前缀 /xx/{idCode}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/873947

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX