compojure defroutes - 路线有时无法识别
我有一个 clojure / compojure web 应用程序,具有以下路由
(defroutes my-routes
(GET "/app/preview" request (my-preview-function request))
(ANY "*" request (str "ANY page <br>" (request :params))))
预览 GET 请求是使用几个参数发出的。我发现这在大多数情况下都有效,但有时找不到 /ebook/preview 并且处理会下降到任何路径,在这种情况下,输出与此类似,
ANY page
{:* "/app/preview", :section "50", :id "48"}
任何人都可以建议可能导致 /ebook/preview 请求的原因跳过?这肯定是一个 GET 请求; HTML 没有 /app/preview URL 的 POST,并且要双重确保我为 /app/preview 添加了 POST 路由,并且没有被命中。
JAR 版本:
Clojure 1.2
compojure-0.6.2
ring-core-0.3.7
jetty-6.1.14
ring-jetty-adapter-0.3.1
ring-servlet-0.3.1jar
servlet-api-2.5-6.1.14
路由包装如下
(require '[compojure.handler :as handler])
(defn wrap-charset [handler charset]
(fn [request]
(if-let [response (handler request)]
(if-let [content-type (get-in response [:headers "Content-Type"])]
(if (.contains content-type "charset")
response
(assoc-in response
[:headers "Content-Type"]
(str content-type "; charset=" charset)))
response))))
(def app (-> my-routes
handler/site
wrap-stateful-session
(wrap-charset "utf-8")
(wrap-file "public")))
(defn run []
(run-jetty (var app) {:join? false :port 8080}))
I have a clojure / compojure webapp with the following routes
(defroutes my-routes
(GET "/app/preview" request (my-preview-function request))
(ANY "*" request (str "ANY page <br>" (request :params))))
The preview GET request is made with a couple of parameters. I find this works most of the time but sometimes the /ebook/preview is not found and processing drops to the ANY route, in which case the output is similar to this,
ANY page
{:* "/app/preview", :section "50", :id "48"}
Can anyone suggest what might cause the /ebook/preview request to be skipped? It is definitely a GET request being made; the HTML does not have a POST for the /app/preview URL and to be doubly sure I added a POST route for /app/preview and that was not being hit.
JAR versions:
Clojure 1.2
compojure-0.6.2
ring-core-0.3.7
jetty-6.1.14
ring-jetty-adapter-0.3.1
ring-servlet-0.3.1jar
servlet-api-2.5-6.1.14
Routes are wrapped as follows
(require '[compojure.handler :as handler])
(defn wrap-charset [handler charset]
(fn [request]
(if-let [response (handler request)]
(if-let [content-type (get-in response [:headers "Content-Type"])]
(if (.contains content-type "charset")
response
(assoc-in response
[:headers "Content-Type"]
(str content-type "; charset=" charset)))
response))))
(def app (-> my-routes
handler/site
wrap-stateful-session
(wrap-charset "utf-8")
(wrap-file "public")))
(defn run []
(run-jetty (var app) {:join? false :port 8080}))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想找出导致问题的请求,请停止使用
(request :params)
丢弃请求映射,只需查看request
即可。这将为您提供一张包含 Compojure 拥有的所有信息的地图;您可以检查它,然后将其传回到您的路线中以观察发生的情况(例如,在进行一些更改之后)。If you're trying to figure out what request is causing the problems, stop throwing away the request map with
(request :params)
and just have a look atrequest
. That will give you a map with all the information Compojure has; you can inspect it, and pass it back into your routes later to observe what happens (after you make some changes, say).如果
返回 nil,则路由将尝试下一条路由。查看(源 GET)并查看它如何匹配(或不匹配)您的路线。
If
returns nil, then the routing will try the next route. Take a look at (source GET) and see how it matches (or doesn't) your route.