Compojure Rest API:以JSON格式显示正文响应

时间:2017-07-11 10:36:34

标签: clojure compojure compojure-api

我是新成员api。

如何以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"
  }
]

1 个答案:

答案 0 :(得分: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))))