如何在 Clojure Liberator 中将 return 401 错误作为 json 格式?

How to return 401 error as a json format in Clojure Liberator?

我正在为 REST 服务使用 liberator,我的内容类型是 json。

:authorized?(或其他决定)失败时 returns text/html 我不想要。

我需要解放者来 return json 数据格式来解决所有类型的错误。

Handlers 可用于自定义在不同情况下要执行的操作,例如句柄正常、未找到资源或用户未通过身份验证。例如:

(resource
  :available-media-types ["application/json"]
  :authorized? (fn [ctx] ...)
  :handle-unauthorized {:message "You need to be authenticated"}
  :exists? (fn [ctx] ...)
  :handle-not-found {:message "Resource not found"})

与其他情况一样,处理程序可以是常量值或生成它的函数。