4

我正在使用 compojure、cheshire 和 korma(以及 postgre db)来创建休息服务。我创建了一个包含两个字符串字段(名称和描述)的表,其结构如下:

(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 没有方法的实现::render of protocol:#'compojure.response/Renderable found for class:clojure.lang.PersistentVector

如我所见,我需要将帖子集合转换为 json。怎么做?

4

1 回答 1

3

响铃响应可以是地图或字符串。如果它们是映射,那么它们使用一些键,例如 :status 和 :body 来定义响应并设置 cookie 等。您可能希望通过将调用包装到(get-all-posts)in来显式地将响应从 Clojure 序列 (edn) 转换为 JSON generate-string(因为您使用的是柴郡):

 {:status 200
  :content-type "application/json; charset=UTF-8"
  :body (cheshire/generate-string (get-all-posts))}

当您使用它时,指定内容类型和响应代码不会有什么坏处。

于 2014-12-19T08:27:55.190 回答