我正在创建一个玩具基座服务,旨在拥有以下资源:
-
/
-
/movies
-
/movies/today
-
/movies/:iso-date
:iso-date
匹配的位置 ####-##-##
最后一个路由的约束使用以下代码片段定义:
^:constraints {:iso-date #"d{4}-d{2}-d{2}"}
每当路由表中存在包含此约束的路由时,我都无法获取其同级路由/movies/today
;相反,我得到的是"未找到"响应。但是,当删除具有约束的路由时,/movies/today
的 GET 将成功。
我使用简洁格式定义的基座路线如下所示:
(defroutes routes
[[["/" {:get root-page}
["/movies" ^:interceptors [fetch-movies]
{:get movies-page}
["/today" {:get movies-for-today-page}]
["/:iso-date" ^:constraints {:iso-date #"d{4}-d{2}-d{2}"}
{:get movies-for-date-page}]]]]])
我是否正确构造了此路由表以实现所需的路由行为?
注意:打印编译的路由给了我预期的结果,因为所有路由都存在生成的正则表达式与 REPL 中预期的:path-re
匹配:
({:path-parts [""],
:path-params [],
:interceptors
[{:name :foobar.service/root-page,
:enter
#object[io.pedestal.interceptor.helpers$before$fn__7359 0x14501070 "io.pedestal.interceptor.helpers$before$fn__7359@14501070"],
:leave nil,
:error nil}],
:path "/",
:method :get,
:path-re #"/QE",
:route-name :foobar.service/root-page}
{:path-parts ["" "movies"],
:path-params [],
:interceptors
[{:name :foobar.service/fetch-movies,
:enter
#object[io.pedestal.interceptor.helpers$on_request$fn__7401 0x2aa85cc4 "io.pedestal.interceptor.helpers$on_request$fn__7401@2aa85cc4"],
:leave nil,
:error nil}
{:name :foobar.service/movies-page,
:enter
#object[io.pedestal.interceptor.helpers$before$fn__7359 0x30ffc3c0 "io.pedestal.interceptor.helpers$before$fn__7359@30ffc3c0"],
:leave nil,
:error nil}],
:path "/movies",
:method :get,
:path-re #"/QmoviesE",
:route-name :foobar.service/movies-page}
{:path-parts ["" "movies" "today"],
:path-params [],
:interceptors
[{:name :foobar.service/fetch-movies,
:enter
#object[io.pedestal.interceptor.helpers$on_request$fn__7401 0x2aa85cc4 "io.pedestal.interceptor.helpers$on_request$fn__7401@2aa85cc4"],
:leave nil,
:error nil}
{:name :foobar.service/movies-for-today-page,
:enter
#object[io.pedestal.interceptor.helpers$before$fn__7359 0x3726fc3b "io.pedestal.interceptor.helpers$before$fn__7359@3726fc3b"],
:leave nil,
:error nil}],
:path "/movies/today",
:method :get,
:path-re #"/QmoviesE/QtodayE",
:route-name :foobar.service/movies-for-today-page}
{:path-parts ["" "movies" :iso-date],
:path-params [:iso-date],
:interceptors
[{:name :foobar.service/fetch-movies,
:enter
#object[io.pedestal.interceptor.helpers$on_request$fn__7401 0x2aa85cc4 "io.pedestal.interceptor.helpers$on_request$fn__7401@2aa85cc4"],
:leave nil,
:error nil}
{:name :foobar.service/movies-for-date-page,
:enter
#object[io.pedestal.interceptor.helpers$before$fn__7359 0x93fb20b "io.pedestal.interceptor.helpers$before$fn__7359@93fb20b"],
:leave nil,
:error nil}],
:path "/movies/:iso-date",
:path-constraints {:iso-date "(\d{4}-\d{2}-\d{2})"},
:query-constraints {},
:method :get,
:path-re #"/QmoviesE/(d{4}-d{2}-d{2})",
:route-name :foobar.service/movies-for-date-page})
我在基座版本 0.4.1-SNAPSHOT 中解决了这个问题。
(io.pedestal.http.route/router my-routes :linear-search)
使用 :linear-search
而不是 :prefix-tree
。