swagger 运行环形服务器的Compojure API引发Map异常错误

daupos2t  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(145)

我正在学习如何使用compojure api的this教程,但由于以下异常,我陷入了死胡同:

lein ring server
2022-08-09 23:19:55.538:INFO::main: Logging initialized @921ms
WARN clojure.tools.logging not found on classpath, compojure.api logging to console.
Exception in thread "main" java.lang.RuntimeException: Map literal must contain an even number of forms, compiling:(ring_test/core.clj:16:16)

除了将我的路线更改为测试路线外,我的所有内容都与教程相同:

(ns ring-test.core
  (:require [compojure.api.sweet :refer :all]
            [ring.util.http-response :refer :all]))

(def app
  (api
   {:swagger
    {:ui "/"
     :spec "swagger.json"
     :data {:info {:tile "Test"}
            :tags [{:name "api"}]}}
    (context "/api" []
      :tags ["api"]
      (GET "/test" []
        :body ["test"]
        (ok)))}))

并更新了project.clj中的一些版本:

(defproject ring-test "0.1.0-SNAPSHOT" 
  :dependencies [[org.clojure/clojure "1.8.0"]
                 [metosin/compojure-api "2.0.0-alpha28"]]
  :ring {:handler ring-test.core/app}
  :profiles {:dev
             {:plugins [[lein-ring "0.12.5"]]
              :dependencies [[javax.servlet/servlet-api "2.5"]]}})

如果有人能帮我解决这个问题,那就太棒了!

s4n0splo

s4n0splo1#

在自学课程中,(context ...)位于Map之外。

(def app
  (api {:swagger
        {:ui   "/"
         :spec "/swagger.json"
         :data {:info {:title "Account Service"}
                :tags [{:name "api"}]}}} ;; END-OF-MAP HERE

       ;; This from is _NOT_ in the map
       (context "/api" []
                :tags ["api"]
                (POST "/account" []
                      :body [account (describe NewAccount "new account")]
                      (ok))
                (POST "/transfer" []
                      :body [transfer (describe NewTransfer "new transfer")]
                      (ok)))))

问题是在你的版本中,你在这个Map中移动(上下文...)。

(def app
  (api {:swagger {:ui "/"
                  :spec "swagger.json"
                  :data {:info {:tile "Test"}
                         :tags [{:name "api"}]}}

        ;; This from is in the map
        (context "/api" []
                 :tags ["api"]
                 (GET "/test" []
                      :body ["test"]
                      (ok)))
        } ;; END-OF-MAP HERE
       ))

您可以通过将(context ...)移动到Map外部来修复此问题,如教程中所示。

相关问题