有没有办法在Ring处理程序中获取HttpServletRequest对象? 我正在使用Noir来开发一个网络应用程序。我需要在处理URI时获取HttpServletRequest obj。所以我使用(noir.request.ring-request)函数来获取响铃请求映射,其中包含:servlet-request键,但值为nil。这是正确的做法还是我错过了什么?
以下是代码:
(ns my-app
(:use noir.request))
(defpage [:get "/app"] []
(str (ring-request)))
结果:
{:remote-addr "127.0.0.1", :scheme :http, :query-params {}, :session {}, :form-params {}, :multipart-params {}, :servlet #, :request-method :get, :query-string nil, :content-type nil, :cookies {"ring-session" {:value "eb509a65-d33a-40d2-9646-e2ff785428b0"}}, :uri "/app", :server-name "127.0.0.1", :params {}, :headers {"cookie" "ring-session=eb509a65-d33a-40d2-9646-e2ff785428b0", "accept-charset" "GBK,utf-8;q=0.7,*;q=0.3", "accept-language" "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4", "accept-encoding" "gzip,deflate,sdch", "accept" "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "user-agent" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2", "connection" "keep-alive", "host" "127.0.0.1:8080"}, :servlet-request #, :content-length nil, :server-port 8080, :character-encoding nil, :servlet-response #, :body #, :servlet-context #}
答案 0 :(得分:1)
看起来响铃请求映射是基于您用于Web服务器的适配器构建的。在jetty的情况下,这发生在ring.adapter.jetty:
https://github.com/ring-clojure/ring/blob/master/ring-jetty-adapter/src/ring/adapter/jetty.clj
如果我是你,我会创建自己的适配器,明确地将您需要的内容添加到请求映射中。具体来说,你会替换
request-map (servlet/build-request-map request)
有类似的东西:
request-map (assoc (servlet/build-request-map request) :jetty-request request)