[英]How to Run Jetty Example with Ring in Clojure
I am following along with this example on creating a simple web service in Clojure using ring and jetty. 我将跟随这个示例 ,使用环和码头在Clojure中创建一个简单的Web服务。
I have this in my project.clj: 我的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"]])
This in script/run.clj 这在script / run.clj中
(use 'ring.adapter.jetty)
(require '[ws-example.web :as web])
(run-jetty #'web/app {:port 8080})
And this in src/ws_example/web.clj 而这在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))
However, when I execute: 但是,当我执行时:
lein run script/run.clj
I get this error: 我收到此错误:
No :main namespace specified in project.clj.
Why am I getting this and how do I fix it? 为什么会得到这个,如何解决?
You're getting this error because the purpose of lein run
(according to lein help run
) is to "Run the project's -main function." 之所以会出现此错误,是因为
lein run
(根据lein help run
)的目的是“运行项目的-main函数”。 You don't have a -main
function in your ws-example.web
namespace, nor do you have a :main
specified in your project.clj
file, which is what lein run
is complaining about. 您的
ws-example.web
命名空间中没有-main
函数,也没有在project.clj
文件中指定:main
,这正是lein run
抱怨的。
To fix this, you have a few options. 要解决此问题,您有一些选择。 You could move the
run-jetty
code to a new -main
function of the ws-example.web
function and then say lein run -m ws-example.web
. 您可以在移动
run-jetty
代码到一个新的-main
的功能ws-example.web
功能,然后说lein run -m ws-example.web
。 Or you could do that and also add a line :main ws-example.web
to project.clj
and then just say lein run
. 或者,您可以这样做,还可以在
project.clj
添加一行:main ws-example.web
,然后直接说lein run
。 Or you could try using the lein exec
plugin to execute a file, rather than a namespace. 或者,您可以尝试使用
lein exec
插件执行文件,而不是名称空间。
For more info, check out the Leiningen Tutorial . 有关更多信息,请查看Leiningen教程 。
您必须将(run-jetty)
内容放入-main
某个位置,然后将其添加到project.clj
:main ws-example.core)
From lein help run
: 从
lein help run
:
USAGE: lein run -m NAMESPACE[/MAIN_FUNCTION] [ARGS...]
Calls the main function in the specified namespace.
So, you would need to put your script.clj
somewhere on the project source path and then call it as: 因此,您需要将
script.clj
放在项目源路径中的某个位置,然后将其称为:
lein run -m script
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.