Clojure 如何将korma select结果转换为rest服务(compojure)的json?
我正在使用compojure、cheshire、korma和postgre db创建rest服务。 我创建了一个表,其中包含两个字符串字段name和description,其结构如下:Clojure 如何将korma select结果转换为rest服务(compojure)的json?,clojure,compojure,korma,sqlkorma,Clojure,Compojure,Korma,Sqlkorma,我正在使用compojure、cheshire、korma和postgre db创建rest服务。 我创建了一个表,其中包含两个字符串字段name和description,其结构如下: (defentity posts (pk :id) (table :posts) (entity-fields :name :description)) 我可以将记录插入此表,但当我尝试执行 (defn get-all-posts [] (select posts)) 并从服务器返回结果 d
(defentity posts
(pk :id)
(table :posts)
(entity-fields :name :description))
我可以将记录插入此表,但当我尝试执行
(defn get-all-posts []
(select posts))
并从服务器返回结果
defroutes app-routes
(GET "/" [] (get-start))
(context "/posts" []
(GET "/" [] (get-all-posts))
...
我收到这样一个错误:
java.lang.IllegalArgumentException
没有为类clojure.lang.PersistentVector实现方法::render of protocol:'compojure.response/Renderable
正如我看到的,我需要将posts集合转换为json。如何操作?可以使用铃声响应。如果它们是一个映射,那么它们会使用一些键,例如:status和:body来定义响应并设置cookies等。由于您使用的是Cheshire:
{:status 200
:content-type "application/json; charset=UTF-8"
:body (cheshire/generate-string (get-all-posts))}
而且,当您进行此操作时,指定内容类型和响应代码并没有什么坏处