clojure - 如何在 Clojure 中使用 Ring 运行 Jetty 示例

标签 clojure jetty leiningen ring

我正在关注 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.webproject.clj然后就说lein run 。或者您可以尝试使用 lein exec plugin执行一个文件,而不是一个命名空间。

有关更多信息,请查看Leiningen Tutorial .

关于clojure - 如何在 Clojure 中使用 Ring 运行 Jetty 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15838917/

相关文章:

java - 在哪里可以找到有关 Jetty API 的优质文档?

java - Haproxy 错误网关 502

clojurescript - 使用 lein figwheel 使所有缓存无效

clojure - 在 clojure 原子交换中执行副作用的正确方法是什么

string - 在 clojure 中应用 str 时保留空格

java - Clojure Android HTTP 请求

linux - 无法为 Ubuntu 安装 LightTable

clojure - 在 Clojure 中定义 SPI

java - SpringBoot 1.2 在关机时抛出 NoClassDefFoundError

opencv - 如何在项目中使用本地jar?