【问题标题】:Using file-response from an uberjar使用来自 uberjar 的文件响应
【发布时间】:2013-09-23 09:11:45
【问题描述】:

从 uberjar 运行时,Ring 的文件响应对我不起作用。我都试过了

(response/file-response "index.html" {:root "resources/public"})

和

(response/response (clojure.java.io/as-file (clojure.java.io/resource "public/index.html")))

如何在 uberjar 中的“/”处提供 index.html?

【问题讨论】:

    标签: web-applications jar clojure compojure ring


    【解决方案1】:

    您需要使用ring.util.response/resource-response,它提供在类路径中找到的文件。假设您要在 überjar 的 resources 目录中提供 index.html 文件,则参数几乎与您对 file-response 的调用完全相同:

    (response/resource-response "index.html" {:root "public"})
    

    它是"public" 而不是"resources/public",因为resources 是包含在überjar 中的顶级目录之一,所以public 将在顶级的类路径中找到。 (如果从文件系统而不是 überjar 运行,这也是正确的;那么路径需要相对于类路径上的某个顶级目录。)

    【讨论】:

    • 有道理,但这对我不起作用。当我点击“/”时,我得到一个 404。我的 index.html 文件在 resources/public 中,我的 project.clj 中没有定义 :resources,所以默认情况下应该是 resources/ 目录吧?
    • 我应该提到 resource-response 返回 Not Found 即使我通过 lein 运行应用程序,而不仅仅是从 uberjar。
    • 啊,对,应该是{:root "public"}。我会在一秒钟内修复答案。
    猜你喜欢
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多