使用时,使用默认值声明可选查询参数的正确方法是什么compojure-api?
我的一个路由元素如下(阅读本文后):
(GET "/:id/descendants" [id]
:return [d/CategoryTreeElement]
:path-params [id :- Long]
:query-params [context-type :- d/ContextType
levels :- Integer
{tenant :- d/Tenant :DEF_TENANT}
{show-future :- Boolean false}
{show-expired :- Boolean false}
{show-suppressed :- Boolean false}
:summary "Fetch category descendants"
(ok ...))
Run Code Online (Sandbox Code Playgroud)
首先,布尔参数定义为其他(例如show-future Boolean),但生成的Swagger UI将它们显示为组合框,其true值为默认值.在当前形式中,UI显示没有选择选项的组合框.租户也是如此.
一方面问题:当我使用Swagger生成的UI发送请求并返回错误时:"levels": "(not (instance? java.lang.Integer \"2\"))".这是为什么?是不是库应该强制/转换字符串值到API声明的指定类型?
提前致谢.
使用compojure-api创建API时,使用body-params和:form-params有什么区别?例如:
(POST* "/register" []
:body-params [username :- String,
password :- String]
(ok)))
Run Code Online (Sandbox Code Playgroud)
VS
(POST* "/register" []
:form-params [username :- String,
password :- String]
(ok)))
Run Code Online (Sandbox Code Playgroud)
哪一个应该用于单页clojurescript应用程序和/或本机移动应用程序将使用的API?
当我有这样的API定义时:
(POST* "/register" []
:body-params [username :- String,
password :- String,
name :- String]
(ok)))
Run Code Online (Sandbox Code Playgroud)
什么是使名称可选的合适方式?是吗:
(POST* "/register" []
:body-params [username :- String,
password :- String,
{name :- String nil}]
(ok)))
Run Code Online (Sandbox Code Playgroud) 例如,Luminus网站说明了这一点
Compojure路由定义只是接受请求映射和返回响应映射的函数......
Run Code Online (Sandbox Code Playgroud)(GET "/" [] "Show something") ...
但是组合路线不是功能
(defmacro GET "Generate a `GET` route."
[path args & body]
(compile-route :get path args body))
Run Code Online (Sandbox Code Playgroud)
可以使用make-route返回函数的函数,但不允许进行解构.因此,作为一个函数,你不能使用compojure的特殊语法来破坏(即向量)但是这会阻止任何形式的解构吗?宏给它们提高了性能吗?
(make-route :get "/some_path" some_handler)
Run Code Online (Sandbox Code Playgroud)
难道不能使用宏包装器将破坏语法传递给函数吗?
不确定为什么当我lein with-profile +live ring uberjar再使用java -jar我的uberjar时,出现此异常:java.lang.NoClassDefFoundError: clojure/lang/Var。
project.clj:
(defproject gn-preview-api "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.9.0"]]
:main gn-preview-api.www.app
:target-path "target/%s"
:profiles {:uberjar {:aot :all}
:staging {:aot :all}
:live {:aot :all}
:dev {:plugins [[lein-ring "0.9.7"]]
:dependencies [[javax.servlet/servlet-api "2.5"]]}})
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
在compojure-api中,我注意到这两种指定资源API的方法:
(POST* "/register" []
:body [user UserRegistration]
(ok)))
Run Code Online (Sandbox Code Playgroud)
和
(POST* "/register" []
:body-params [username :- String,
password :- String]
(ok)))
Run Code Online (Sandbox Code Playgroud)
这两者有什么区别?使用一个与另一个有什么含义?
我正在尝试学习Clojure,并且在文字函数语法方面受阻.我无法弄清楚文字函数的等价物(defn fourteen [] 14)是什么.
(def fourteen (fn [] 14))
;; => #'user/fourteen
(fourteen)
;; => 14
(defn defn-fourteen [] 14)
;; => #'user/defn-fourteen
(defn-fourteen)
;; => 14
(def literal-14 #(14))
;; => #'user/literal-14
(literal-14)
;; ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn user/literal-14 (form-init2956929406616221071.clj:1)
Run Code Online (Sandbox Code Playgroud)
我不认为这是一个重复的匿名函数在clojure中期望多少个参数?,但也许它是,我只是没有经验来认识到这一点.
我如何或者可以使用def literal-14来允许(literal-14)调用工作?
如何在compojure中绑定动态变量?请参阅下面的示例,这里request-id是为每个api请求生成的唯一uuid.我希望能够访问该请求ID在随后的方法记录等等.我已经使用绑定功能试过,但我仍然无法访问请求ID的some-page/some-method.
handler.clj
(ns some_app.handler
(:require
[compojure.api.sweet :refer :all]
[compojure.route :as route]
[some_api.some_page :as some-page]))
(def ^:dynamic *request-id*
nil)
(defn ^:private generate-request-id []
(str (java.util.UUID/randomUUID)))
(def app
(binding [*request-id* (generate-request-id)]
(api
(context "/api" [] (GET "/some-page" [] (some-page/some-method))))))
Run Code Online (Sandbox Code Playgroud)
一些-page.clj
(ns some_app.some_page
(:require
[clojure.tools.logging :as log]))
(def some-method []
(log/info {:request-id *request-id*}))
Run Code Online (Sandbox Code Playgroud) 我传递路径参数来从数据库中获取数据.
终点
http://localhost:3000/hello/1000
GET方法代码
码
(ns clojure-dauble-business-api.core
(:require [compojure.api.sweet :refer :all]
[ring.util.http-response :refer :all]
[clojure-dauble-business-api.logic :as logic]
[clojure.tools.logging :as log]
[clojure-dauble-business-api.domain.artwork])
(:import [clojure_dauble_business_api.domain.artwork Artwork]))
(defapi app
(GET ["/hello/:id", :id #"[0-9]+"] [id]
(log/info "Function begins from here" id)
(ok {:artwork (logic/artwork-id id)})))
Run Code Online (Sandbox Code Playgroud)
当我从邮递员到达终点时,我收到此错误,
org.postgresql.util.PSQLException: ERROR: operator does not exist: integer = character varying
Run Code Online (Sandbox Code Playgroud)
我已经知道值id作为String值传递给查询.
在传递给Query之前,将Path参数类型更改为Number的最佳方法是什么?
我希望能够json编码异常对象.我正在将我的日志推送到sumologic并且希望能够推送json编码的异常,以便我可以在sumo中解析和过滤这些日志.
但是,我不能json编码异常并得到此错误:
Cannot JSON encode object of class: class java.lang.Class: class clojure.lang.ExceptionInfo
Run Code Online (Sandbox Code Playgroud)
这是我的compojure-api异常处理程序:
(defn exception-handler
"Handles exceptions."
[f]
(fn [^Exception ex data request]
(log/error (json/generate-string {:request-id log-helper/*request-id*
:error ex}))
(f (.getMessage ex))))
Run Code Online (Sandbox Code Playgroud) 我现在开始进行函数式编程,而且在没有变量的情况下工作变得非常疯狂.
我读过的每个教程都说它不是很酷的重新定义变量,但我不知道如何在不保存变量状态的情况下解决我的实际问题.
例如:我正在研究API,我希望在请求中保留值.让我们说我有一个端点,添加一个person,我有一个列表persons,我想redefine或改变我的persons列表添加新的值person.我怎样才能做到这一点?
可以使用var-set,alter-var-root或者conj!?
(对于api我正在使用compojure-api,每个person都是一个Hash)