[英]How do i server static html file with ring handler in clojure?
I am trying to server html over a ring based api server path .我正在尝试通过基于环的 api 服务器路径来服务器 html。 however everytime i hit the endpoint , i am getting this wierd error
然而每次我到达终点时,我都会收到这个奇怪的错误
java.lang.IllegalArgumentException: No implementation of method: :write-body-to-stream of protocol: #'ring.core.protocols/StreamableResponseBody found for class: clojure.lang.PersistentArrayMap
at clojure.core$_cache_protocol_fn.invokeStatic(core_deftype.clj:583) ~[clojure-1.10.1.jar:?]
at clojure.core$_cache_protocol_fn.invoke(core_deftype.clj:575) ~[clojure-1.10.1.jar:?]
at ring.core.protocols$eval18503$fn__18504$G__18494__18513.invoke(protocols.clj:8) ~[?:?]
at ring.util.servlet$update_servlet_response.invokeStatic(servlet.clj:106) ~[?:?]
at ring.util.servlet$update_servlet_response.invoke(servlet.clj:91) ~[?:?]
at ring.util.servlet$update_servlet_response.invokeStatic(servlet.clj:95) ~[?:?]
at ring.util.servlet$update_servlet_response.invoke(servlet.clj:91) ~[?:?]
at ring.adapter.jetty$proxy_handler$fn__18623.invoke(jetty.clj:27) ~[?:?]
Here is what my api is returning.这是我的 api 返回的内容。
{:status 200
:body (resource-response "index.html" {:root "public"})}
Whereas if i hit the index.html path directly it is accessible at this route而如果我直接点击 index.html 路径,它可以在这条路线上访问
http://localhost:8080/index.html
You get the error No implementation of method: :write-body-to-stream of protocol: #'ring.core.protocols/StreamableResponseBody found for class: clojure.lang.PersistentArrayMap
because as a body you return a PersistentArrayMap instead of something that can be encoded as the body of a Ring HTTP response.您收到错误
No implementation of method: :write-body-to-stream of protocol: #'ring.core.protocols/StreamableResponseBody found for class: clojure.lang.PersistentArrayMap
因为作为主体返回 PersistentArrayMap 而不是某些东西可以编码为 Ring HTTP 响应的主体。
resource-response
already returns a full response map (a PersistentArrayMap): resource-response
已经返回一个完整的响应映射(一个 PersistentArrayMap):
(resource-response "index.html")
;; => {:status 200,
;; :headers
;; {"Content-Length" "0", "Last-Modified" "Mon, 16 Nov 2020 14:22:48 GMT"},
;; :body
;; #object[java.io.File 0x239d3777 "/index.html"]}
so no need to wrap it in {:status 200, :body ...}
since it becomes {:status 200 :body {:status 200, ...}}
which lead to that error.所以不需要将它包装在
{:status 200, :body ...}
因为它变成了{:status 200 :body {:status 200, ...}}
这会导致该错误。 To fix it your API can directly return:要修复它,您的 API 可以直接返回:
(resource-response "index.html" {:root "public"})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.