如何将korma选择结果转换为json以获得休息服务(compojure)?

时间:2014-12-19 07:30:04

标签: clojure compojure korma sqlkorma

我使用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 for class:clojure.lang.PersistentVector

我认为我需要将posts集合转换为json。怎么做?

1 个答案:

答案 0 :(得分:3)

响铃响应可以是either a map or a string。如果它们是地图,那么它们使用一些键,例如:status和:body来定义响应并设置cookie等。您可能希望通过将调用包装到{显式地将响应从Clojure序列(edn)转换为JSON。 {1}}中的{1}}(因为您使用的是Cheshire):

(get-all-posts)

当你在它时,指定内容类型和响应代码并没有什么坏处。