PUT 时渲染资源

Render resource when PUT

我正在使用 liberator 使用 Clojure 构建 API。给定以下代码:

(defresource single-customer [id]
  :allowed-methods [:get, :put]
  :exists? (fn [_]
             (let [e (get @cust/customers (keyword id))]
               (if-not (nil? e)
                 {::entry e})))
  :existed? (fn [_] (nil? (get @cust/customers (keyword id) ::sentinel)))
  :available-media-types ["application/json"]
  :can-put-to-missing? false
  :put! (fn [q] (cust/set-as-fraudulent id))
  :handle-ok ::entry)

有人可以告诉我是否可以,比如 GET 请求,当我发送 PUT 请求时它被重定向到资源? "/customer/1"(例如)?

查看 liberator decision graph:put! 可能导致:

  • :handle-created(如果:new?
  • :handle-no-content(如果不是 :new? 也不是 :respond-with-entity?
  • :handle-ok(如果不是:new,而是:respond-with-entity?

尝试实现 :put!,以便将实体存储为 ::entry,并且 :handle-created 类似于您当前的 :handle-ok