Spark-Java:不同的路径字符串映射到相同的获取方法
Spark-Java: Different Path Strings Map to Same Get Method
我 运行 遇到了两个不同的路径映射到同一资源的问题。请告诉我为什么以下 2 条路径映射到同一路径:
get("/test/:idtest/:idsimple", (request, response) -> "");
get("/test/all/:idtest", (request, response) -> "");
以下两个调用映射相同:
curl -X GET -i http://localhost:4567/test/2/3
curl -X GET -i http://localhost:4567/test/all/5
谢谢
这两个请求映射到第一个路由的原因是您定义它们的顺序。 Spark Java 文档提到 here:
Routes are matched in the order they are defined. The first route that matches the request is invoked.
当您调用 http://localhost:4567/test/2/3
Java 时,Spark 会首先尝试将其与您定义的第一个路由进行匹配 "/test/:idtest/:idsimple"
:
- 变量
idtest
将匹配到 2
- 变量
idsimple
将匹配到 3
当您调用 http://localhost:4567/test/all/5
Java 时,Spark 将首先尝试将其与您再次定义的第一个路由相匹配:
- 变量
idtest
将匹配到 all
- 变量
idsimple
将匹配到 5
所以它们都匹配并因此映射到这条路线。
如果您更改路由定义的顺序,那么 "/test/all/:idtest"
将是第一个要匹配的路径,然后调用 http://localhost:4567/test/all/5
将映射到正确的路由,而调用 http://localhost:4567/test/2/3
将使第一个失败并映射到第二个。
我 运行 遇到了两个不同的路径映射到同一资源的问题。请告诉我为什么以下 2 条路径映射到同一路径:
get("/test/:idtest/:idsimple", (request, response) -> "");
get("/test/all/:idtest", (request, response) -> "");
以下两个调用映射相同:
curl -X GET -i http://localhost:4567/test/2/3
curl -X GET -i http://localhost:4567/test/all/5
谢谢
这两个请求映射到第一个路由的原因是您定义它们的顺序。 Spark Java 文档提到 here:
Routes are matched in the order they are defined. The first route that matches the request is invoked.
当您调用 http://localhost:4567/test/2/3
Java 时,Spark 会首先尝试将其与您定义的第一个路由进行匹配 "/test/:idtest/:idsimple"
:
- 变量
idtest
将匹配到2
- 变量
idsimple
将匹配到3
当您调用 http://localhost:4567/test/all/5
Java 时,Spark 将首先尝试将其与您再次定义的第一个路由相匹配:
- 变量
idtest
将匹配到all
- 变量
idsimple
将匹配到5
所以它们都匹配并因此映射到这条路线。
如果您更改路由定义的顺序,那么 "/test/all/:idtest"
将是第一个要匹配的路径,然后调用 http://localhost:4567/test/all/5
将映射到正确的路由,而调用 http://localhost:4567/test/2/3
将使第一个失败并映射到第二个。