2017-07-11 60 views
1

我是compojure api的新手。Compojure Rest API:以JSON格式顯示body響應

如何以JSON格式顯示身體響應?

這是我寫的代碼

core.clj

(ns clojure-dauble-business-api.core 
    (:require [compojure.api.sweet :refer :all]) 
    (:require [ring.util.http-response :refer :all]) 
    (:require [clojure-dauble-business-api.dbdev :as dbdev]) 
    (:require [yesql.core :refer [defquery]]) 
    (:require [cheshire.core :as json]) 
    (:require [ring.util.response :as response]) 
    (:require [clojure-dauble-business-api.logic :as logic]) 
    (:require [clojure.tools.logging :as log]) 
    (:require [clojure-dauble-business-api.artwork :as artwork])) 

(defapi app 
    (GET "/hello" [] 
    (log/info "Function begins from here" 
    :return [artwork/artwork] 
    :summary "Returns list of artworks" 
    (response/response (logic/artworks))))) 

的代碼輸出爲:

:return [{:id java.lang.Integer, #schema.core.OptionalKey{:k :name} java.lang.String}] 
:summary Returns list of artworks 
{:status 200, :headers {}, :body [{"id":25,"name":"Garden"},{"id":27,"name":"Lord Of the Rings Statue"},{"id":32,"name":"DEFAULT"},{"id":33,"name":"Garden"},{"id":39,"name":"garden"},{"id":83,"name":"yyeye"},{"id":86,"name":"DEFAULT"},{"id":88,"name":"wera"},{"id":137,"name":""},{"id":149,"name":"DEFAULT"}]} 

我怎麼能顯示出響應的:body成JSON格式。

我需要這樣

[ 
    { 
    "id": 25, 
    "name": "Garden" 
    }, 
    { 
    "id": 27, 
    "name": "Lord Of the Rings Statue" 
    }, 
    { 
    "id": 32, 
    "name": "DEFAULT" 
    }, 
    { 
    "id": 33, 
    "name": "Garden" 
    }, 
    { 
    "id": 39, 
    "name": "garden" 
    }, 
    { 
    "id": 83, 
    "name": "yyeye" 
    }, 
    { 
    "id": 86, 
    "name": "DEFAULT" 
    }, 
    { 
    "id": 88, 
    "name": "wera" 
    }, 
    { 
    "id": 137, 
    "name": "" 
    }, 
    { 
    "id": 149, 
    "name": "DEFAULT" 
    } 
] 
+0

我得到了我的錯誤。我錯誤地在日誌裏面寫了''(log/info「函數從這裏開始」 :return [藝術作品/藝術作品] :summary「藝術品返回列表」 (response/response(logic/artwork)))))'' ' – Srini

回答

1

我得到了答案,想通了,我做錯了,顯示的數據。

(ns clojure-dauble-business-api.core 
    (:require [compojure.api.sweet :refer :all]) 
    (:require [ring.util.http-response :refer :all]) 
    (:require [clojure-dauble-business-api.dbdev :as dbdev]) 
    (:require [yesql.core :refer [defquery]]) 
    (:require [cheshire.core :as json]) 
    (:require [ring.util.response :as response]) 
    (:require [clojure-dauble-business-api.logic :as logic]) 
    (:require [clojure.tools.logging :as log]) 
    (:require [clojure-dauble-business-api.artwork :as artwork])) 

(defapi app 
    (GET "/hello" [] 
    (log/info "Function begins from here" 
    :return [artwork/artwork] 
    :summary "Returns list of artworks" 
    (response/response (logic/artworks))))) 

應該

(ns clojure-dauble-business-api.core 
    (:require [compojure.api.sweet :refer :all]) 
    (:require [ring.util.http-response :refer :all]) 
    (:require [clojure-dauble-business-api.dbdev :as dbdev]) 
    (:require [yesql.core :refer [defquery]]) 
    (:require [cheshire.core :as json]) 
    (:require [ring.util.response :as response]) 
    (:require [clojure-dauble-business-api.logic :as logic]) 
    (:require [clojure.tools.logging :as log]) 
    (:require [clojure-dauble-business-api.artwork :as artwork])) 

(defapi app 
    (GET "/hello" [] 
    (log/info "Function begins from here") 
    :return [artwork/artwork] 
    :summary "Return list of artworks" 
    (ok (logic/artworks)))) 
+0

另外,請記住,您不需要:每次使用庫時都需要。它可以簡單地爲(:require [library1:全部引用] [library2:refer [abc]]) – Slae