带有 Compojure 默认值的可选查询参数(没有招摇)?

Optional query parameters with default value with Compojure (without swagger)?

我在 Compojure 中处理可选查询参数的惯用方式是什么,并将未定义的查询参数分配给默认值。

我已经试过了(这可能显然行不通):

(GET "/something/:id" [id q :<< as-int :or {q (System/currentTimeMillis)}]
    ....)

我希望这条路线同时匹配:

curl /something/123

curl /something/123?q=234542345

(注意类似的问题已发布 但它使用 Swagger)

您有很多选择。这是我可能会选择的。

(GET "/something/:id" req
  (let [{:keys [id q] :or {q (System/currentTimeMillis)}} (:params req)]

    ,,,))

不过,最终,我选择哪一个将取决于导致最具可读性(主观测量,但这就是它的方式)代码的内容。