我在Clojure中构建了一个非常简单的Web应用程序(实际上我遵循了一个教程)。 project.clj文件如下所示:
(defproject webdev "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[ring "1.2.2"]
[hiccup "1.0.5"]
[compojure "1.1.6"]
[org.clojure/java.jdbc "0.3.3"]
[postgresql/postgresql "9.1-901.jdbc4"]]
:plugins [[lein-ring "0.8.10"]]
:ring {:handler (webdev.core/-main)
:port 8000}
:uberjar-name "webdev.jar"
:main webdev.core
:aot [webdev.core]
:profiles {:dev
{:main webdev.core/-dev-main}})
我的主要webdev / core.clj的相关部分如下所示:
(defroutes routes
(GET "/about" [] about)
(ANY "/request" [] handle-dump)
(GET "/items" [] handle-index-items)
(POST "/items" [] handle-create-item)
(DELETE "/items/:item-id" [] handle-delete-item)
(PUT "/items/:item-id" [] handle-update-item)
(not-found "Page not found."))
(defn wrap-db [hndlr]
(fn [req]
(hndlr (assoc req :webdev/db db))))
(defn wrap-server-header [hndlr]
(fn [req]
(hndlr (assoc req :webdev/db db))))
(defn wrap-server-response [hndlr]
(fn [req]
(let [response (hndlr req)]
(assoc-in response [:headers "Server:"] "my-server"))))
(def sim-methods {"PUT" :put
"DELETE" :delete})
(defn wrap-simulated-methods [hndlr]
(fn [req]
(if-let [method (and (= :post (:request-method req))
(sim-methods (get-in req [:params "_method"])))]
(hndlr (assoc req :request-method method))
(hndlr req))))
(def app
(wrap-file-info
(wrap-resource
(wrap-server-response
(wrap-db
(wrap-params
(wrap-simulated-methods routes)))) "static")))
(defn -main [& [port]]
(items/create-table db)
(jetty/run-jetty #'app
{:port (if port (Integer/parseInt port)
(Integer/parseInt (System/getenv "PORT")))}))
(defn -dev-main [port]
(items/create-table db)
(jetty/run-jetty (wrap-reload #'app) {:port (Integer. port)}))
....
如果我在项目上运行Lein ring server
或Lein ring uberjar
,我会收到以下错误:
java.lang.ClassCastException: clojure.lang.PersistentList
cannot be cast to clojure.lang.Named
如果我运行Lein run 8000
。它工作正常,我可以浏览到localhost:8000
我的Leiningen版本是:
Leiningen 2.3.4 on Java 1.6.0_26 Java HotSpot(TM) 64-Bit Server VM
任何人都知道什么是错的?
此致 西蒙
答案 0 :(得分:3)
那看起来不错。
:ring {:handler (webdev.core/-main)
:port 8000}
(webdev.core / -main)表示运行main函数并将main函数的结果作为处理程序委托给ring-plugin。
使用lein ring时尝试使用更像这样的东西......
:ring {:handler webdev.core/app
:port 8000}