运行Ring服务器的Compojure API抛出Map字面量异常求助
解决Compojure API运行
lein ring server时的Map Literal异常 你的错误是代码存在三个语法/结构问题,导致编译器解析map字面量时出错:
def语法错误:def app不符合Clojure定义变量的语法,必须写成(def app ...)- Swagger配置Map未闭合:
api函数的第一个参数是配置map,你的代码里:swagger对应的map结束后未添加},直接跟(context ...),导致整个结构被解析为不完整的map(键值对数量为奇数) - Swagger字段拼写错误:
:info {:tile "Test"}中的:tile应为:title,这是Swagger规范要求的字段名,写错会导致Swagger UI无法正常显示
修正后的路由代码
(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 {:title "Test"} :tags [{:name "api"}]}}} ; 补上闭合的} (context "/api" [] :tags ["api"] (GET "/test" [] :body ["test"] (ok)))))
可选优化:消除日志警告
启动时的clojure.tools.logging not found警告,可通过在project.clj中添加对应依赖解决:
(defproject ring-test "0.1.0-SNAPSHOT" :dependencies [[org.clojure/clojure "1.8.0"] [metosin/compojure-api "2.0.0-alpha28"] [org.clojure/tools.logging "1.2.4"]] ; 添加该依赖 :ring {:handler ring-test.core/app} :profiles {:dev {:plugins [[lein-ring "0.12.5"]] :dependencies [[javax.servlet/servlet-api "2.5"]]}})
修改后重新运行lein ring server即可正常启动服务。
内容的提问来源于stack exchange,提问作者Jack Gee
相关产品推荐
相关产品推荐

