compojure defroutes - 路线有时无法识别

发布于 2024-12-20 20:53:30 字数 1377 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宣告ˉ结束 2024-12-27 20:53:30

如果您想找出导致问题的请求,请停止使用 (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 at request. 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).

橘香 2024-12-27 20:53:30

如果

(my-preview-function request)

返回 nil,则路由将尝试下一条路由。查看(源 GET)并查看它如何匹配(或不匹配)您的路线。

If

(my-preview-function request)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文