简体   繁体   English

我如何在 clojure 中使用环处理程序服务器静态 html 文件?

[英]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.

相关问题 在clojure / ring中,如何删除cookie? - In clojure/ring, how do I delete a cookie? clojure / ring / jetty:我正在使用> lein环形服务器。 如何配置实例化的码头实例? - clojure/ring/jetty: I am using > lein ring server. How do I configure the jetty instance that gets instantiated? 您如何在 clojure 环服务器中提供动态创建的文件? - How do you serve dynamically created files in clojure ring server? clojure / lein / ring:我有两个处理不同功能的环形处理程序,如何将其包装到servlet中? - clojure/lein/ring: I have two ring handlers doing different things, how do I wrap this into a servlet? 带有clojure和ring的静态文件 - Static files with clojure and ring 如何使用ring server构建clojure应用程序 - How to build clojure application with ring server Clojure Ring:如何确定开发服务器是否正在运行? - Clojure Ring: How to determine if development server is running? 如何在Clojure中编写静态文件服务器? - How to write a static file server in Clojure? 如何关闭在ElasticBeanstalk中部署到tomcat的Clojure Ring Web应用程序中的Hikari连接池 - How do I shutdown Hikari connection pool in clojure ring web app deployed to tomcat in elasticbeanstalk Clojure Ring / Compojure REPL中的动态处理程序更新 - Dynamic handler update in Clojure Ring/Compojure REPL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM