You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ring服务器对PersistentArrayMap返回空响应,LazySeq正常,如何解决?

解决Ring返回PersistentArrayMap时空响应的问题

问题根源

空响应的核心原因是直接返回Clojure的PersistentArrayMap不符合Ring的响应规范。Ring中间件(包括wrap-json-response)要求处理的是标准Ring响应结构(包含:body、:status等键的Map),而非原始数据结构。LazySeq能正常工作是因为wrap-json-response对序列类型做了自动包装兼容,但Map没有这个默认处理逻辑。

两种可行修复方案

方案1:用Ring工具函数包装响应

使用ring.util.response/response函数,它会自动把原始数据转换成标准Ring响应(默认状态码200,数据放入:body字段)。

首先在代码中引入工具包:

(require '[ring.util.response :as resp])

然后修改路由处理逻辑:

(defroutes app-routes 
  (GET "/get-data" [type] (resp/response (api/get-users type))))

方案2:手动构造标准Ring响应

如果需要灵活控制响应状态码等参数,可以直接构造符合规范的响应Map:

(defroutes app-routes 
  (GET "/get-data" [type] 
    {:status 200 
     :body (api/get-users type)}))

验证效果

修改后,无论返回LazySeq还是PersistentArrayMap,wrap-json-response都会正确识别:body中的数据并序列化为JSON。比如你测试的{:latest 202410-hu, :past [{:id 202212-nl-ch-fr}]}会被转换为:

{"latest": "202410-hu", "past": [{"id": "202212-nl-ch-fr"}]}

内容的提问来源于stack exchange,提问作者LocustHorde

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 02:57:08