4

残りのサービスを作成するために、compojure、cheshire、および korma (および postgre db) を使用しています。このような構造を持つ 2 つの文字列フィールド (名前と説明) を持つテーブルを作成しました。

(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 No implementation of method: :render of protocol: #'compojure.response/Renderable found for class: clojure.lang.PersistentVector

ご覧のとおり、投稿コレクションをjsonに変換する必要があります。どうやってするの?

4

1 に答える 1

3

リング応答は、マップまたは文字列のいずれかです。(get-all-posts)それらがマップの場合、:status や :body などのいくつかのキーを使用して応答を定義し、Cookie などを設定します。呼び出しをin にラップして、Clojure シーケンス (edn) から JSON に応答を明示的に変換することをお勧めします。generate-string(Cheshireを使用しているため):

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

そして、コンテンツ タイプと応答コードを指定することは問題ありません。

于 2014-12-19T08:27:55.190 に答える