Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure JSON:异常:com.fasterxml.jackson.core.jsongGenerationException:无法对类的对象进行JSON编码:_Json_Clojure_Neo4j_Ring_Cheshire - Fatal编程技术网

Clojure JSON:异常:com.fasterxml.jackson.core.jsongGenerationException:无法对类的对象进行JSON编码:

Clojure JSON:异常:com.fasterxml.jackson.core.jsongGenerationException:无法对类的对象进行JSON编码:,json,clojure,neo4j,ring,cheshire,Json,Clojure,Neo4j,Ring,Cheshire,我试图使用clojure neocons在我的Neo4j数据存储中创建一个节点,但我遇到了一个错误,我认为这与格式化json有关: Exception: com.fasterxml.jackson.core.JsonGenerationException: Cannot JSON encode object of class: class recursiftion.dao_graph$create_node: recursiftion.dao_graph$create_node@32693b5

我试图使用clojure neocons在我的Neo4j数据存储中创建一个节点,但我遇到了一个错误,我认为这与格式化json有关:

Exception: com.fasterxml.jackson.core.JsonGenerationException: Cannot JSON encode object of class: class recursiftion.dao_graph$create_node: recursiftion.dao_graph$create_node@32693b5
                      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]
...
根据错误报告,我怀疑这可能与柴郡有关。但是我很困惑,因为我没有在发生错误的文件中包括柴郡图书馆

我将此作为包装器,因为我正在发出cors POST&GET请求,并且需要从controller.clj返回JSON

(def app 
(-> (handler/api app-routes)
(middleware/wrap-json-body {:keywords? true})
(middleware/wrap-json-response)
(wrap-cors routes #"^http://localhost:9000$")))
(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 "/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)
}
))
下面是my controller.clj中引用的库

(def app 
(-> (handler/api app-routes)
(middleware/wrap-json-body {:keywords? true})
(middleware/wrap-json-response)
(wrap-cors routes #"^http://localhost:9000$")))
(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 "/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)
}
))
这是我的controller.clj中的POST端点代码

(def app 
(-> (handler/api app-routes)
(middleware/wrap-json-body {:keywords? true})
(middleware/wrap-json-response)
(wrap-cors routes #"^http://localhost:9000$")))
(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 "/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]) })))
这是一个cypher查询,它返回一个以“…”为键的映射

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

我非常感谢您在解决这个问题上的帮助。

信息确实不够。然而,当cheshire不识别被告知编码为json的数据类型时,通常会出现这种异常。该错误看起来像是您试图对函数进行json编码,而不是对函数的输出进行json编码?我会在端点中添加一个println,并打印出您认为返回的内容。如果输出看起来像一个函数对象,而不是json可以编码的东西,这可能就是原因


至于发生这种情况的地方,我猜您的中间件正在尝试将响应体编码为json

是的,信息确实不够。 但是,根据您的异常,很明显cheshire不知道类型“recursiftion.dao\u graph$create\u node” “这就是它抛出此异常的原因

例如,简单的解决方案是在发送到柴郡之前将其解析为已知数据

(defn my-parser
     [data]
(assoc data :index_with_not_know_type (str (:index_with_not_know_type data))))
Obs:在我的例子中,我只是将带有\u not\u know\u类型值的:index\u强制转换为字符串

然后,在你的情况下,你可以这样做

(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)
          (my-parser))
}
))


你需要做一个更简单的例子,让人们能够有效地帮助你。这篇文章已经收到了一些赞成票,所以似乎还有其他人也有同样的问题。如果我在这个问题上加上一个悬赏,你能帮我吗?我能有效帮助你的唯一方法是你创建一个我能运行的最小失败的例子。理想情况下,这将是在这里,并在一个开放源码的回购某处,我可以克隆和运行。赏金固然不错,但我也需要更多的信息:)