我正在关注 this example使用ring和jetty在Clojure中创建一个简单的Web服务。
我的project.clj中有这个:
(defproject ws-example "0.0.1"
:description "REST datastore interface."
:dependencies
[[org.clojure/clojure "1.5.1"]
[ring/ring-jetty-adapter "0.2.5"]
[ring-json-params "0.1.0"]
[compojure "0.4.0"]
[clj-json "0.5.3"]]
:dev-dependencies
[[lein-run "1.0.0-SNAPSHOT"]])
这在 script/run.clj 中
(use 'ring.adapter.jetty)
(require '[ws-example.web :as web])
(run-jetty #'web/app {:port 8080})
这在 src/ws_example/web.clj
(ns ws-example.web
(:use compojure.core)
(:use ring.middleware.json-params)
(:require [clj-json.core :as json]))
(defn json-response [data & [status]]
{:status (or status 200)
:headers {"Content-Type" "application/json"}
:body (json/generate-string data)})
(defroutes handler
(GET "/" []
(json-response {"hello" "world"}))
(PUT "/" [name]
(json-response {"hello" name})))
(def app
(-> handler
wrap-json-params))
但是,当我执行时:
lein run script/run.clj
我收到此错误:
No :main namespace specified in project.clj.
为什么我会遇到这个问题以及如何修复它?
最佳答案
您收到此错误是因为 lein run
的目的(根据 lein help run
)是“运行项目的 -main 函数”。您没有-main
在您的 ws-example.web
中运行命名空间,你也没有 :main
在您的 project.clj
中指定文件,这就是 lein run
正在提示。
要解决此问题,您有几种选择。您可以移动 run-jetty
代码为新的-main
ws-example.web
的功能函数然后说 lein run -m ws-example.web
。或者您可以这样做并添加一行 :main ws-example.web
至project.clj
然后就说lein run
。或者您可以尝试使用 lein exec
plugin执行一个文件,而不是一个命名空间。
有关更多信息,请查看Leiningen Tutorial .
关于clojure - 如何在 Clojure 中使用 Ring 运行 Jetty 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15838917/