我在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 serverLein ring uberjar,则会出现以下错误:
java.lang.ClassCastException: clojure.lang.PersistentList
cannot be cast to clojure.lang.Named

如果我运行Lein run 8000。它可以正常工作,我可以浏览到localhost:8000

我的雷宁根版本是:
Leiningen 2.3.4 on Java 1.6.0_26 Java HotSpot(TM) 64-Bit Server VM

有人知道怎么了吗?

问候,
西蒙

最佳答案

看起来错了。

 :ring {:handler (webdev.core/-main)
         :port 8000}

(webdev.core / -main)意味着运行您的main函数,并将main Function的结果委派为环形插件的处理程序。

使用lein环时,尝试使用更多类似的东西...
 :ring {:handler webdev.core/app
         :port 8000}

关于clojure - Lein ring uberjar,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23222145/

10-11 10:39