2015-09-25 79 views
3

我想创建一个节点在我的Neo4j数据存储使用clojure新手,我遇到了一个错误,我认为是与格式化json相关:Clojure JSON:异常:com.fasterxml.jackson.core.JsonGenerationException:不能JSON编码类的对象:

Exception: com.fasterxml.jackson.core.JsonGenerationException: Cannot JSON encode object of class: class recursiftion.dao_graph$create_node: [email protected] 
         generate.clj:148 cheshire.generate/generate 
         generate.clj:119 cheshire.generate/generate 
          core.clj:31 cheshire.core/generate-string 
          core.clj:21 cheshire.core/generate-string 
         cypher.clj:51 clojurewerkz.neocons.rest.cypher/query 
         core.clj:2440 clojure.core/comp[fn] 
        dao_graph.clj:428 recursiftion.dao-graph/create-node 
         model.clj:131 recursiftion.model/createNode 
        controller.clj:206 recursiftion.controller/fn 
          core.clj:99 compojure.core/make-route[fn] 
          core.clj:45 compojure.core/if-route[fn] 
          core.clj:30 compojure.core/if-method[fn] 
          core.clj:112 compojure.core/routing[fn] 
         core.clj:2570 clojure.core/some 
          core.clj:112 compojure.core/routing 
         RestFn.java:139 clojure.lang.RestFn.applyTo 
          core.clj:632 clojure.core/apply 
          core.clj:117 compojure.core/routes[fn] 
       keyword_params.clj:32 ring.middleware.keyword-params/wrap-keyword-params[fn] 
... 

我怀疑这可能与基于错误报告的柴郡有关。但是我很困惑,因为我没有在发生错误的文件中包含Cheshire库。

我有这个作为我的包装,因为我是做CORS POST & GET请求,我需要从我controller.clj

(def app 
(-> (handler/api app-routes) 
(middleware/wrap-json-body {:keywords? true}) 
(middleware/wrap-json-response) 
(wrap-cors routes #"^http://localhost:9000$"))) 

下面返回JSON的图书馆在我controller.clj引用

(ns recursiftion.controller 
(:use [ring.adapter.jetty :only [run-jetty]] 
[recursiftion.websocket :only [wamp-handler]] 
[recursiftion.config :only [conf]] 
) 
(:require [compojure.core :refer :all] 
[compojure.handler :as handler] 
[compojure.route :as route] 
[clojure.java.io :as io] 
[ring.util.io :refer [string-input-stream]] 
[ring.util.response :as resp] 
[ring.util.response :refer [response]] 
[ring.middleware.json :as middleware] 
[ring.middleware.cors :refer [wrap-cors]] 
[environ.core :refer [env]] 
[cheshire.core :refer :all] 
[recursiftion.model :as model] 
[monger.json] 
[clojure.pprint :refer [pprint]])) 

这里是我的POST端点的代码在我controller.clj

(POST "/node/create" request 
(let [node_object (or (get-in request [:params :data]) 
(get-in request [:body :data]) 
"1ROUTER_ERROR")] 
{:status 200 
:headers {"Content-Type" "application/json"} 
:body (recursiftion.model/createNode node_object) 
} 
)) 

以下是通过我的模型,并在dao_graph.clj

(ns recursiftion.dao_graph 
(:require [clojure.string] 
[clojurewerkz.neocons.rest :as nr] 
[clojurewerkz.neocons.rest.cypher :as cy] 
[clojure.pprint :refer [pprint]]) 
(:import [org.bson.types.ObjectId]) 
) 

在这里被发现的引用的库是内部dao_graph.clj调用该函数的定义:

(defn create-node [ nodeobj ] 
(let [ _nodeobj nodeobj ] 
(cy/tquery conn create-node { :_nodetype (get-in _nodeobj [:type]) }))) 

这里是返回一个地图,我对解决这个问题的帮助非常感激的暗号查询“......”作为其键

(def create-node "CREATE (m:{_nodetype}) 
RETURN M;") 

+0

您需要为人们提供一个更简单的例子,以便能够有效地帮助您。 –

+0

这篇文章已经收到了一些upvotes,所以似乎还有其他人也有同样类型的麻烦。如果我为这个问题增加赏金,你能帮忙吗? –

+1

我可以有效帮助的唯一方法是如果您创建一个可以运行的最小失败示例。理想情况下,这将在这里,并在一个开放源代码回购的地方,我可以克隆和运行。赏金会很好,但我需要更多信息:) –

回答

1

没有足够的信息。但是,当柴郡不能识别被告知要编码为json的数据类型时,通常会看到这种异常。该错误看起来像你可能试图json编码一个函数而不是从函数输出?我会在您的端点中添加一个println,并打印出您相信正在返回的内容。如果输出看起来像一个函数对象而不是json可以编码的东西,那可能是原因。

至于发生这种情况,我的猜测是你的中间件试图将响应主体编码为json。