Jac*_*ant 6 json clojure jodatime ring
使用以下应用程序:
; src/webapp/core.clj
(ns webapp.core
(:require [compojure.core :refer [defroutes GET]]
[ring.middleware.json :as mid-json]
[clj-time.jdbc]))
(defn foo [request]
{:body {:now (org.joda.time.DateTime/now)}})
(defroutes routes
(GET "/foo" [] foo))
(def app
(-> routes
(mid-json/wrap-json-response)))
Run Code Online (Sandbox Code Playgroud)
点击/ foo端点给我这个错误:
com.fasterxml.jackson.core.JsonGenerationException:JSON不能编码类的对象:class org.joda.time.DateTime:2017-10-21T03:38:16.207Z
是否有一种简单的方法可以让ring-json对DateTime对象进行编码?我是否必须编写自己的中间件才能将其转换为例如字符串?如果是这样,我该怎么做?(我以前从未写过环中间件).
我的project.clj有这些依赖项FYI:
[[org.clojure/clojure "1.8.0"]
[org.clojure/java.jdbc "0.6.1"]
[ring/ring-jetty-adapter "1.4.0"]
[compojure "1.4.0"]
[ring/ring-json "0.4.0"]
[clj-time "0.14.0"]]
Run Code Online (Sandbox Code Playgroud)
如果您使用Cheshire生成JSON,您可以扩展其协议以处理序列化,然后它应该"正常工作":
(extend-protocol cheshire.generate/JSONable
org.joda.time.DateTime
(to-json [dt gen]
(cheshire.generate/write-string gen (str dt))))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
635 次 |
| 最近记录: |