1

我希望我能以一种有意义的方式解释这一点!

我正在使用 Liberator 对一些需要向客户端公开的 Web 服务进行原型设计,并具有如下定义的路由:

(defroutes fish
  (context "/fish"
           []
           (ANY "/cod/:id/count"
                [id]
                (cod-fish id))))

(def handler
  (-> fish
      wrap-params
      path-wrapper))

的目的path-wrapper是输出一些关于匹配路径的信息。它目前看起来像这样:

(defn path-wrapper
  [handler]
  (fn [request]
    (println "in" (:request-method request) (:uri request))
    (let [response (handler request)]
      (println "out")
      response)))

这会打印出您所期望的:

in :get /fish/cod/123/count
out

但是,我希望它打印出来的是:

in :get /fish/cod/:id/count
out

也就是说,匹配的路径而不是匹配它的 URI。

我几乎可以肯定答案在某个地方,但我似乎无法找到它!:(

有什么建议吗?

干杯,

彼得

4

1 回答 1

0

在这种情况下,我喜欢放入调试语句,例如:

(let [response .... ]
    (log/errorf "in: request was: %s" 
       (with-out-str (clojure.pprint/pprint request))
 ....

并在输出中查找您想要的数据(然后删除该语句),或者如果您有一个工作和现代的 emacs+cider 环境,您可以向函数添加调试并以C-uC-cC-c这种方式捕获请求的值。如果您不想要的数据可用,它可能会在该输出中。如果您不使用日志框架,则删除 log 和 with-out-str 部分,然后直接调用 pprint 。

抱歉,如果我误解了,或者问题中可能是错字:

(let [response handler]
      (println "out")
      response)

看起来它正在返回处理程序本身,而不是调用该处理程序的结果,应该是:

(let [response (handler request)]
      (println "out")
       response)  
于 2015-09-17T22:41:23.967 回答