1

我是compojure api的新手。

如何以 JSON 格式显示正文响应?

这是我写的代码

核心.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"
  }
]
4

1 回答 1

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))))
于 2017-07-11T10:48:30.393 回答