Clojure和Compojure:响应图为零

时间:2019-03-30 05:51:09

标签: clojure

我正在将compojure用于基本的Web应用程序,我的代码在core.clj中:

(defroutes routes
      (GET "/" [] (layout/application "Home" (contents/index)))
      (route/resources "/"))

(def application (handler/site routes))

(defn -main []
  (let [port (Integer/parseInt (or (System/getenv "PORT") "8090"))]
    (jetty/run-jetty application {:port port :join? false})))

当我访问0.0.0.0:8090时,一切正常,但是我仍然看到此错误:

java.lang.NullPointerException: Response map is nil
    at ring.util.servlet$update_servlet_response.invokeStatic(servlet.clj:100)
    at ring.util.servlet$update_servlet_response.invoke(servlet.clj:91)
    at ring.util.servlet$update_servlet_response.invokeStatic(servlet.clj:95)
    at ring.util.servlet$update_servlet_response.invoke(servlet.clj:91)
    at ring.adapter.jetty$proxy_handler$fn__337.invoke(jetty.clj:27)
    at ring.adapter.jetty.proxy$org.eclipse.jetty.server.handler.AbstractHandler$ff19274a.handle(Unknown Source)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
    at org.eclipse.jetty.server.Server.handle(Server.java:503)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:364)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:305)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)
    at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:333)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:310)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:168)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:126)
    at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:366)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:765)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:683)
    at java.base/java.lang.Thread.run(Thread.java:844)

知道发生了什么吗?

1 个答案:

答案 0 :(得分:1)

@Svante几乎可以肯定是正确的。一种简单的验证方法是使用the Tupelo library中的spyx函数:

(ns demo.core
  (:use tupelo.core))

(defroutes routes
  (GET "/" [] (spyx (layout/application "Home" (contents/index))))
  (route/resources "/"))

它将打印类似的内容:

(layout/application "Home" (contents/index))) => nil 

运行时。 spyx(“显式间谍”)打印您提供的表达式,箭头和表达式值。 spyspyxspy-pretty 也返回打印的值 (不同于println总是返回{{ 1}}),因此您可以在任何地方插入nil打印输出而不会中断处理链。因此,您无需编写如下内容:

spy

以获取打印的调试消息。为了与(defroutes routes (GET "/" [] (let [tmp-1 (layout/application "Home" (contents/index))] (println "layout/application result => " tmp-1) tmp-1))) (route/resources "/")) 和朋友,请将其添加到spy中的:dependencies中:

project.clj

更新

嗯...。不确定可能是什么问题。我使用以下内容从[tupelo "0.9.138"] 制作了一个简单的演示应用程序:

lein new compojure demo-compojure

和结果:

(ns demo-compojure.handler
  (:use tupelo.core)
  (:require [compojure.core :refer :all]
            [compojure.route :as route]
            [ring.middleware.defaults :refer [wrap-defaults site-defaults]]))

(defn index []
  (spy :index--result "Hello World"))

(defroutes app-routes
  (GET "/" [] (spyx (index)))
  (route/not-found "Not Found"))

(def app
  (wrap-defaults app-routes site-defaults))

这是可行的。此外,~/expr/demo-compojure > lein ring server SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Started server on port 3000 :index--result => "Hello World" (index) => "Hello World" 是Clojure的基本功能,这令人困惑。也许像上面那样做一个干净的演示项目,然后从那里拿走?